Boost C++ Libraries: Ticket #7962: shared_ptr of chat_server.cpp https://svn.boost.org/trac10/ticket/7962 <p> In the example file "<a href="http://www.boost.org/doc/libs/1_52_0/doc/html/boost_asio/example/chat/chat_server.cpp">chat_server.cpp</a>" <a href="http://www.boost.org/doc/libs/1_52_0/doc/html/boost_asio/example/chat/chat_server.cpp">http://www.boost.org/doc/libs/1_52_0/doc/html/boost_asio/example/chat/chat_server.cpp</a> </p> <p> In the for-loop in the main: </p> <blockquote> <p> chat_server_ptr server(new chat_server(io_service, endpoint)); </p> </blockquote> <p> while </p> <blockquote> <p> typedef boost::shared_ptr&lt;chat_server&gt; chat_server_ptr; </p> </blockquote> <p> Since a shared_ptr is used to manage chat_server, class chat_server should inherit public boost::enable_shared_from_this&lt;chat_server&gt;, just as class chat_session does. </p> <p> Or in the member function start_accept() of class chat_server: </p> <blockquote> <p> acceptor_.async_accept( </p> <blockquote> <p> new_session-&gt;socket(), boost::bind(&amp;chat_server::handle_accept, this, new_session, </p> <blockquote> <p> boost::asio::placeholders::error)); </p> </blockquote> </blockquote> </blockquote> <p> The appearance of "this" seems problematic. </p> <p> Thanks for reading. Shiwei Weng </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/7962 Trac 1.4.3 chris_kohlhoff Fri, 24 May 2013 04:12:52 GMT status changed; resolution set https://svn.boost.org/trac10/ticket/7962#comment:1 https://svn.boost.org/trac10/ticket/7962#comment:1 <ul> <li><strong>status</strong> <span class="trac-field-old">new</span> → <span class="trac-field-new">closed</span> </li> <li><strong>resolution</strong> → <span class="trac-field-new">wontfix</span> </li> </ul> <p> In this case, the chat server is only put in a shared_ptr so that it may be stored in a std::list. For example, in C++11 this may be eliminated in favour of using std::list::emplace_back. It's lifetime outlasts io_service::run() so there isn't a need to tie the lifetime to the asynchronous operations it performs. </p> Ticket