title: shared_from_this causing bad_weak_ptr
body: <p>I am trying to keep a list of connected clients in asio. I have adapted the chat server example from the docs (<a href="http://www.boost.org/doc/libs/1_57_0/doc/html/boost_asio/example/cpp03/chat/chat_server.cpp" rel="noreferrer">http://www.boost.org/doc/libs/1_57_0/doc/html/boost_asio/example/cpp03/chat/chat_server.cpp</a>) and here's the important part of what I ended up with:</p>
  <pre><code>#include &lt;iostream&gt;
  #include &lt;boost/bind.hpp&gt;
  #include &lt;boost/shared_ptr.hpp&gt;
  #include &lt;boost/enable_shared_from_this.hpp&gt;
  #include &lt;boost/asio.hpp&gt;
  #include &lt;set&gt;
  
  using boost::asio::ip::tcp;
  
  class tcp_connection;
  
  std::set&lt;boost::shared_ptr&lt;tcp_connection&gt;&gt; clients;
  
  void add_client(boost::shared_ptr&lt;tcp_connection&gt; client)
  {
      clients.insert(client);
  }
  
  class tcp_connection : public boost::enable_shared_from_this&lt;tcp_connection&gt;
  {
  public:
      tcp_connection(boost::asio::io_service&amp; io_service) : socket_(io_service)
      {
      }
  
      tcp::socket socket_;
  
      void start()
      {
          add_client(shared_from_this());
      }
  
      tcp::socket&amp; socket()
      {
          return socket_;
      }
  };
  
  class tcp_server
  {
  public:
      tcp_server(boost::asio::io_service&amp; io_service)
          : io_service_(io_service),
          acceptor_(io_service, tcp::endpoint(tcp::v4(), 6767))
      {
          tcp_connection* new_connection = new tcp_connection(io_service_);
          acceptor_.async_accept(new_connection-&gt;socket(),
                               boost::bind(&amp;tcp_server::start_accept, this, new_connection,
                                           boost::asio::placeholders::error));
      }
  
  private:
      void start_accept(tcp_connection* new_connection,
                        const boost::system::error_code&amp; error)
      {
          if (!error)
          {
              new_connection-&gt;start();
              new_connection = new tcp_connection(io_service_);
              acceptor_.async_accept(new_connection-&gt;socket(),
                                     boost::bind(&amp;tcp_server::start_accept, this, new_connection,
                                                 boost::asio::placeholders::error));
          }
      }
  
      boost::asio::io_service&amp; io_service_;
      tcp::acceptor acceptor_;
  };
  
  int main()
  {
      try
      {
          boost::asio::io_service io_service;
          tcp_server server(io_service);
          io_service.run();
      }
      catch (std::exception&amp; e)
      {
          std::cerr &lt;&lt; &quot;Exception: &quot; &lt;&lt; e.what() &lt;&lt; &quot;\n&quot;;
      }
  
      return 0;
  }
  </code></pre>
  <p>At the call to <code>shared_from_this()</code>, my server crashes with the message:</p>
  <blockquote>
  <p>Exception: tr1::bad_weak_ptr</p>
  </blockquote>
  <p>I have done some searching and it appears <code>shared_from_this()</code> is pretty particular, but I can't seem to find exactly what I need to change.</p>