title: object containing smart pointer to itself that isn't reset before object goes out of scope
body: <p>Ran into this bug where a class contains a pointer to itself -- and it was pointing to the same instance. Here is the smallest code snippet that contains that problem... </p>
  
  <pre><code>class Node
  {
  public:
      Node()
      {
          std::cout &lt;&lt; "constructor" &lt;&lt; std::endl;
      }
  
      ~Node()
      {
          std::cout &lt;&lt; "destructor" &lt;&lt; std::endl;
      }
  
      void storeChild(boost::shared_ptr&lt;Node&gt; ptr)
      {
          m_ptr = ptr;
      }
  
  private: 
      boost::shared_ptr&lt;Node&gt; m_ptr;
  };
  
  int main()
  {
      boost::shared_ptr&lt;Node&gt; bc_ptr(new Node());
      bc_ptr-&gt;storeChild(bc_ptr); // this line is the bug 
      return 0;
  }
  </code></pre>
  
  <p>My questions are:
  1. Is the Node object ever deleted? I know smart pointers are supposed to manage those things so we don't have to delete. But it looks like the reference that's passed into the class using <code>storeChild</code> never is reset. Does this mean I'm going to have a memory leak? 
  2. Is there a way to use smart pointers that prevents this from happening? Clearly the <code>storeChild</code> method should be given a pointer to a different Node but how do you prevent that? </p>
  
  <p>If I run this program, the destructor never gets called. </p>