[thread] Segfault constructing a thread
I've got a singleton object that is trying to run its operator() method in a separate thread, but I'm getting a segfault when I construct the thread object: PublishedListener::PublishedListener(string directory) { _thread = new thread(*this); // segfault here } PublishedListener.h: class PublishedListener { public: static PublishedListener &getInstance(); void run(); void operator()(); boost::filesystem::path getItemDirectory(); virtual ~PublishedListener(); private: PublishedListener(std::string directory); static PublishedListener *_instance; boost::thread *_thread; boost::filesystem::path *itemDirectory; }; Thanks in advance!
On 4/27/07, Ken Klose
I've got a singleton object that is trying to run its operator() method in a separate thread, but I'm getting a segfault when I construct the thread object:
PublishedListener::PublishedListener(string directory) { _thread = new thread(*this); // segfault here }
PublishedListener.h: class PublishedListener { public: static PublishedListener &getInstance(); void run(); void operator()(); boost::filesystem::path getItemDirectory(); virtual ~PublishedListener(); private: PublishedListener(std::string directory); static PublishedListener *_instance; boost::thread *_thread; boost::filesystem::path *itemDirectory; };
Thanks in advance!
Are you sure the implementation of operator()() isnt seg faulting, the thread should be able to invoke it properly and i dont see any reason for the thread instantiation to segfault.. HTH Digz
Ken Klose wrote:
I've got a singleton object that is trying to run its operator() method in a separate thread, but I'm getting a segfault when I construct the thread object:
PublishedListener::PublishedListener(string directory) { _thread = new thread(*this); // segfault here }
This line makes a copy of *this. It's very likely that this isn't what you want. Use ref(*this) to store a reference, and consider disabling the copy constructor and the assignment operator of the class.
participants (3)
-
Digvijoy Chatterjee
-
Ken Klose
-
Peter Dimov