title: C++ boost::asio how to properly use std::shared_ptr on async functions
body: <p>This is my first project with <code>boost::asio</code> and I´m building an async server.</p>
  
  <p>I need to use a <code>shared_ptr</code> to control access to the <code>session</code> control pointer. Here is the way I´m doing to define the session and pass it to the boost async function:</p>
  
  <pre><code>// Define the new session object
  std::shared_ptr&lt;SocketSession&gt; session = std::make_shared&lt;SocketSession&gt;(ioService);
  
  // Configure the acceptor
  acceptor.async_accept(session-&gt;getSessionSocket(),
                        boost::bind(&amp;SocketServer::HandleAccept, 
                        this, 
                        session, 
                        boost::asio::placeholders::error));
  </code></pre>
  
  <p>And the handler signature:</p>
  
  <pre><code>void SocketServer::HandleAccept(std::shared_ptr&lt;SocketSession&gt; session,
                                  const boost::system::error_code&amp; errorCode)
  </code></pre>
  
  <p>The code compile fine, not errors at all. But on running the code, the <code>HandleAccept</code> method is called with an error: <code>Operation canceled</code>, not even listening to the socket.</p>
  
  <p>It seens that this is related to the way I´m using <code>shrared_ptr</code>. I´ve gone through some examples and boost uses <code>shared_from_this</code>. I can´t find out why is that necessary, and why my <code>shared_ptr</code> fail at runtime.</p>
  
  <p>Help appreciated.</p>
  
  <p>Note: This code is running on ther server class (<code>Server</code>), not on the session class (<code>SocketSession</code>). In my view this lead not to use <code>shared_from_this</code> as <code>SocketSession</code> is being created at another object....</p>