Boost logo

Boost Users :

Subject: Re: [Boost-users] shared_ptr to this
From: Mateusz Loskot (mateusz_at_[hidden])
Date: 2013-09-09 17:59:07


In your st, sruct Data, make the ptr a shared_ptr with custom deleter which
does nothing.

Mateusz Loskot
(Sent from mobile, apology for top-posting and broken quotes)
On 9 Sep 2013 21:55, "Gonzalo Garramuno" <ggarra13_at_[hidden]> wrote:

> I have an application where I need to pass "this" in a callback belonging
> to a class to a function using a shared_ptr in another thread
>
> In code:
>
> struct Data
> {
> Data( void* p ) :
> ptr( p )
> {
> }
>
> boost::shared_ptr<void> ptr;
> };
>
> void threadA( Data* d )
> {
> boost::shared_ptr<void> ptr( d->ptr );
> delete d;
> // use ptr
> }
>
> class A
> {
> void init_thread()
> {
> Data* data = new Data( this );
> boost.thread t( boost::bind( threadA, data ) );
> }
> };
>
> The problem is obvious. Once threadA closes, the shared ptr is destroyed
> as its count is 0 and as it points to the this of class A this gets deleted
> too.
> However my class A is still active and should not get deleted. How can I
> work around this?
>
> ______________________________**_________________
> 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