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?
The
shared_ptrapproach is fairly common. However, instead of passing ashared_ptras an additional argument tobind, it is possible to pass theshared_ptras the instance object in place ofthis.Often, since the instance is going to be managed via a
shared_ptr, which may or may not be in the context ofthis, it is a good idea to use Boost.SmartPointer’senable_shared_from_this. When a class inherits fromboost::enable_shared_from_this, it provides ashared_from_this()member function that returns a validshared_ptrinstance tothis.In this snippet,
foo.get()andfoo->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 inProtocol::AsyncReadMessageifAsyncReadHandler‘s copy-constructor throws when trying to invokeAsyncReadMessage. Boost.Asio’s asynchronous TCP daytime server and many of the examples showenable_shared_from_thisbeing used within Boost.Asio. For a deeper understanding, this question specifically covers asynchronous Boost.Asio functions andshared_ptr.Also, in the original code, it may be easier to make
Protocol::AsyncHelpera template class instead of having it be a non-templated class with template member functions. This would allow theAsyncHelperto accept the protocol, stream, and handler as constructor arguments, storing them as member variables. Additionally, it makes thebindcalls 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.