Boost logo

Boost Users :

Subject: Re: [Boost-users] boost::asio destructor segfault?
From: Sean McAllister (smcallis_at_[hidden])
Date: 2010-03-19 10:25:40


The only functions I'm binding with boost::bind are members of
ClassifierContext, eg:

ICMessage::async_read(socket_,
                              boost::bind(&ClassifierContext::handleCmdMsg,
this,
                     _1,_2,_3));

Since the io_service object is stopped in the destructor of
ClassifierContext, shouldn't that be OK? Or should
I still try to create a shared_from_this off of the ClassifierContext class?

On Thu, Mar 18, 2010 at 11:50 PM, Rutger ter Borg <rutger_at_[hidden]>wrote:

> 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 mailing list
> Boost-users_at_[hidden]
> http://lists.boost.org/mailman/listinfo.cgi/boost-users
>



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