title: bad_weak_ptr when calling shared_from_this() in base class
body: <p>I have a <code>SuperParent</code> class, a <code>Parent</code> class (derived from <code>SuperParent</code>) and both contain a <code>shared_ptr</code> to a <code>Child</code> class (which contains a <code>weak_ptr</code> to a <code>SuperParent</code>). Unfortunately, I'm getting a <code>bad_weak_ptr</code> exception when trying to set the <code>Child</code>'s pointer. The code is the following:</p>
  
  <pre><code>#include &lt;boost/enable_shared_from_this.hpp&gt;
  #include &lt;boost/make_shared.hpp&gt;
  #include &lt;boost/shared_ptr.hpp&gt;
  #include &lt;boost/weak_ptr.hpp&gt;
  
  using namespace boost;
  
  class SuperParent;
  
  class Child {
  public:
      void SetParent(shared_ptr&lt;SuperParent&gt; parent)
      {
          parent_ = parent;
      }
  private:
      weak_ptr&lt;SuperParent&gt; parent_;
  };
  
  class SuperParent : public enable_shared_from_this&lt;SuperParent&gt; {
  protected:
      void InformChild(shared_ptr&lt;Child&gt; grandson)
      {
          grandson-&gt;SetParent(shared_from_this());
          grandson_ = grandson;
      }
  private:
      shared_ptr&lt;Child&gt; grandson_;
  };
  
  class Parent : public SuperParent, public enable_shared_from_this&lt;Parent&gt; {
  public:
      void Init()
      {
          child_ = make_shared&lt;Child&gt;();
          InformChild(child_);
      }
  private:
      shared_ptr&lt;Child&gt; child_;
  };
  
  int main()
  {
      shared_ptr&lt;Parent&gt; parent = make_shared&lt;Parent&gt;();
      parent-&gt;Init();
      return 0;
  }
  </code></pre>