
Lloyd wrote:
Creating asynchronous operation handlers on stack does not make much sense.
Why do you say this? Is there any chance of crashing the application?
Correction of my previous statement: It does not make sense to pass member function bound to a stack allocated object as an asynchronous operation handler. Passed handler must remain valid until async operation is completed i.e. until it gets called. The shared_ptr trick is a way to ensure that, because handler itself owns a shared pointer to the object in question thus assuring its still valid. Your example is perfectly safe because it will do nothing - you are not running io_service in a separate thread, so async operation will never even start. You might want to have a look at asynchronous examples in asio documentation.