c++ - shared_ptr gets destroyed before I can use it properly -
i have following code, supposed add shared_ptr instance intrusive linked list thread a. other consumer thread use later on removing list. @ point shared_ptr seems destroyed , reference in linked list no longer valid resulting in assert.
can enlighten me on doing wrong? suspect because create original shared_ptr in local scope , gets destroyed. although list should still have reference it??? (this not production code)
edit:
defintiions of variables used:
basehookqueuelist* lst; typedef boost::intrusive::list<queuelist> basehookqueuelist; class queuelist : public boost::intrusive::list_base_hook<> // making basehookqueuelist take shared_ptr of pointing type queuelist conflicts // list_base_hook because not seem shared_ptr... //////////////////////////////////////////////////////////////////////////////////// if (dwwait == wait_object_0) //wait successfull { while(count != 100) { //some new request arrived boost::shared_ptr<queuelist> qlist (new queuelist()); //create shared_ptr instance int temp = 0; if (count > 5) { qlist->setname(names[temp]); // fill name property of queuelist object nonsense... temp++; } else { qlist->setname(names[count]); } workerfunc(lst, qlist); // pass worker func count++; } } // shared_ptr goes out scope , assert error list!! } } void workerfunc(basehookqueuelist* l, list item) // list typedef shared_ptr<queuelist> list { { boost::mutex::scoped_lock workerfunclock(listlock); l->push_front(*item); //add list } //scope of lock { boost::mutex::scoped_lock workerfuncconsolelock(consolelock); printf("adding item list...\n"); } releasesemaphore(hconsumer, 1, null); // set conumser semaphore signalled number of items on queue }
if basehookqueuelist intrusive list said, should remember intrusive list doesn't take ownership of objects. in case shared pointers have ownership, , when destroyed, object destroyed too.
edit: instead of intrusive list use container std::list or std::queue can contain smart pointers.
Comments
Post a Comment