9 Sep
2013
9 Sep
'13
8:55 p.m.
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?