Boost logo

Boost Users :

Subject: Re: [Boost-users] shared_ptr to this
From: Gonzalo Garramuno (ggarra13_at_[hidden])
Date: 2013-09-09 17:05:41


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;
}



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