The shared_ptr
approach is fairly common. However, instead of passing a shared_ptr
as an additional argument to bind
, it is possible to pass the shared_ptr
as the instance object in place of this
.
boost::shared_ptr< my_class > ptr( this );
boost::asio::async_read( stream, buffer,
boost::bind( &my_class::read_handler, ptr,
boost::asio::placeholders::error
boost::asio::placeholders::bytes_transferred ) );
Often, since the instance is going to be managed via a shared_ptr
, which may or may not be in the context of this
, it is a good idea to use Boost.SmartPointer's enable_shared_from_this
. When a class inherits from boost::enable_shared_from_this
, it provides a shared_from_this()
member function that returns a valid shared_ptr
instance to this
.
class my_class: public boost::enable_shared_from_this< my_class >
{
void read()
{
boost::asio::async_read( stream, buffer,
boost::bind( &my_class::read_handler, shared_from_this(),
boost::asio::placeholders::error
boost::asio::placeholders::bytes_transferred ) );
}
};
boost::shared_ptr< my_class > foo( new my_class() );
foo->read();
In this snippet, foo.get()
and foo->shared_from_this()
both point to the same instance. This helps prevent difficult to locate memory leaks. For example, in the original example code, a memory leak occurs in Protocol::AsyncReadMessage
if AsyncReadHandler
's copy-constructor throws when trying to invoke AsyncReadMessage
. Boost.Asio's asynchronous TCP daytime server and many of the examples show enable_shared_from_this
being used within Boost.Asio. For a deeper understanding, this question specifically covers asynchronous Boost.Asio functions and shared_ptr
.
Also, in the original code, it may be easier to make Protocol::AsyncHelper
a template class instead of having it be a non-templated class with template member functions. This would allow the AsyncHelper
to accept the protocol, stream, and handler as constructor arguments, storing them as member variables. Additionally, it makes the bind
calls slightly easier to read since it reduces the amount of arguments needing to be passed and since the member functions are no longer templates, there is no need to specify their full type. Here is a quick pass at an example.