Here's a compilable version of the program that shows the crash due
to the ref. count reaching 0 in the thread.
#include <boost/bind.hpp>
#include <boost/thread/thread.hpp>
#include <time.h>
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
{
public:
void init_thread()
{
Data* data = new Data( this );
boost::thread t( boost::bind( threadA, data ) );
}
void sleep()
{
timespec req;
req.tv_sec = 3;
req.tv_nsec = 0;
nanosleep( &req, NULL );
}
};
int main()
{
A a;
a.init_thread();
a.sleep();
return 0;
}