
19 Mar
2010
19 Mar
'10
6:50 a.m.
Sean McAllister wrote:
Not using a shared_ptr_from_this, but I am using shared_ptr wrapped objects as parameters to my asynchronous callbacks...
If you use patterns like boost::bind( &some_class::some_mem_fn, this, .... ) then it could be that the instance of some_class is destructed before the bind-expression is. If the handler is called with "operation cancelled" (which should happen with all waiting handlers if you call io_service.stop()), it tries to make a call into freed memory -- you can expect a crash, indeed. To solve this, one can use shared_from_this(), boost::bind( &some_class::some_mem_fn, shared_from_this(), .... ) to make sure the object's lifetime will be at least that of the incomplete handler. Cheers, Rutger