Boost logo

Boost Users :

Subject: Re: [Boost-users] boost::asio destructor segfault?
From: Rutger ter Borg (rutger_at_[hidden])
Date: 2010-03-19 02:50:34


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


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net