title: Why can't I use shared_from_this with a callbacks interface object?
body: <p>I'm having trouble with a bad weak_ptr exception when I try to pass a shared_from_this() from a derived class that inherits from the callback interface into a third class which takes the callback base class shared_ptr in the constructor. Why doesn't this work? See simplified test case below:</p>
  
  <pre><code>#include &lt;boost/tr1/memory.hpp&gt;
  
  class MyCallbacks : public std::tr1::enable_shared_from_this&lt;MyCallbacks&gt;
  {
  public:
    virtual ~MyCallbacks() {}
    virtual void callback1() = 0;
  };
  
  class Worker
  {
  public:
    Worker(std::tr1::shared_ptr&lt;MyCallbacks&gt; cb) : callbacks(cb) {}
  
    void do_work() { if (callbacks) callbacks-&gt;callback1(); }
  
  private:
    std::tr1::shared_ptr&lt;MyCallbacks&gt; callbacks;
  
  };
  
  class Boss : public MyCallbacks
  {
  public:
    Boss() : worker(shared_from_this()) {}
  
    void go()
    {
      std::cerr &lt;&lt; "Calling worker-&gt;do_work" &lt;&lt; std::endl;
      worker.do_work();
    }
  
    void callback1()
    {
      std::cerr &lt;&lt; "callback1" &lt;&lt; std::endl;
    }
  
  private:
    Worker worker;
  };
  
  int main (int argc, char const *argv[])
  {
    std::tr1::shared_ptr&lt;Boss&gt; boss(new Boss);
    boss-&gt;go();
    return 0;
  }
  </code></pre>