C++ asynchronous call best practice
boost-asio, c++
Solution
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.
Problem
I'm working with boost::asio. I wrote a class responsible for asynchronous reading from socket. In my application io_service can be stopped and started many times during one application run. So I have to worry about memory leakage when service is stopped. I came to two solutions: A class asking for asynchronous request provides function with a buffer for using in asio reads, and it is responsible for its freeing. This is an obvious solution but I don't like it. Passing a parameter you don't need to a function looks realy strange. Smart pointer binded to a callback. Example here: http://pastebin.com/p8nQ5NFi Now I'm using the second solution but however I'm feeling, I'm inventing a wheel. What's a common practice for buffer cleanage in asynchronous call? Are there any hidden problems in my aproach?