Note that there are some explanatory texts on larger screens.

plurals
  1. POAvoiding indirect cyclic references when using shared_ptr and weak_ptr
    primarykey
    data
    text
    <p>I'm currently putting together an application that relies heavily on <a href="http://www.boost.org/doc/libs/1_41_0/libs/smart_ptr/smart_ptr.htm" rel="nofollow noreferrer"><code>shared_ptr</code></a> and everything looks good so far - I've done my <a href="https://stackoverflow.com/questions/701456/what-are-potential-dangers-when-using-boostsharedptr">homework</a> and have a pretty good idea of some of the pitfalls of using <code>shared_ptr</code>s.</p> <p>One of the most recognised problems with <code>shared_ptr</code> is cyclic dependencies - these issues can be solved by storing <code>weak_ptr</code>s that don't affect the lifetime of objects up the chain. However, I'm struggling to get my head around times where it's necessary to store a pointer to an external object via a <code>weak_ptr</code> - <strong>I'm not sure whether it's forbidden, discouraged, or whether it's safe</strong>.</p> <p>The following diagram describes what I mean (black arrows indicate <code>shared_ptr</code>; dashed indicate <code>weak_ptr</code>):</p> <p><a href="http://img694.imageshack.us/img694/6628/sharedweakptr.png" rel="nofollow noreferrer">alt text http://img694.imageshack.us/img694/6628/sharedweakptr.png</a></p> <ul> <li>A parent contains <code>shared_ptr</code>s to two children, both of which point back to the parent using a <code>weak_ptr</code>. </li> <li>In the constructor of the <em>first child</em> I retrieve via the parent <code>weak_ptr</code> the pointer to the <em>second child</em> and store it locally.</li> </ul> <p>The code looks like this:</p> <pre><code>#include &lt;boost/shared_ptr.hpp&gt; #include &lt;boost/weak_ptr.hpp&gt; #include &lt;boost/make_shared.hpp&gt; #include &lt;boost/enable_shared_from_this.hpp&gt; class child; class child2; class parent; class parent : public boost::enable_shared_from_this&lt;parent&gt; { public: void createChildren() { _child2 = boost::make_shared&lt;child2&gt;(shared_from_this()); _child = boost::make_shared&lt;child&gt;(shared_from_this()); } boost::shared_ptr&lt;child&gt; _child; boost::shared_ptr&lt;child2&gt; _child2; }; class child { public: child(boost::weak_ptr&lt;parent&gt; p) { _parent = p; _child2 = boost::shared_ptr&lt;parent&gt;(p)-&gt;_child2; // is this safe? } boost::weak_ptr&lt;parent&gt; _parent; boost::shared_ptr&lt;child2&gt; _child2; }; class child2 { public: child2(boost::weak_ptr&lt;parent&gt; p) { this-&gt;_parent = p; } boost::weak_ptr&lt;parent&gt; _parent; }; int main() { boost::shared_ptr&lt;parent&gt; master(boost::make_shared&lt;parent&gt;()); master-&gt;createChildren(); } </code></pre> <p>I've tested this and it <em>seems</em> to work ok (I don't get any reports of memory leaks), however my question is: <strong>Is this safe? And if not, why not?</strong></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload