|
Boost Users : |
Subject: [Boost-users] shared_ptr to this
From: Gonzalo Garramuno (ggarra13_at_[hidden])
Date: 2013-09-09 16:55:20
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 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