Note that there are some explanatory texts on larger screens.

plurals
  1. POshared_ptr gets destroyed before I can use it properly
    text
    copied!<p>I have the following code, which is supposed to add a shared_ptr instance to an intrusive linked list by thread A. Some other consumer thread will then use it later on by removing it from the list. However at a certain point my shared_ptr seems to get destroyed and the reference to it in the linked list is no longer valid resulting in an Assert.</p> <p>Can someone enlighten me on what I am doing wrong? I suspect it is because I create my original shared_ptr in a local scope and it just gets destroyed. Although the List should still have a reference to it??? (This is NOT production code)</p> <p>EDIT:</p> <p><strong>Defintiions of variables used:</strong></p> <pre><code>BaseHookQueueList* lst; typedef boost::intrusive::list&lt;QueueList&gt; BaseHookQueueList; class QueueList : public boost::intrusive::list_base_hook&lt;&gt; // Making my BaseHookQueueList take a shared_ptr of pointing to type QueueList conflicts // with the list_base_hook because it does not seem to like shared_ptr... //////////////////////////////////////////////////////////////////////////////////// if (dwWait == WAIT_OBJECT_0) //Wait is successfull { while(count != 100) { //Some new request arrived boost::shared_ptr&lt;QueueList&gt; Qlist (new QueueList()); //Create shared_ptr instance int temp = 0; if (count &gt; 5) { Qlist-&gt;SetName(names[temp]); // Fill up name property of QueueList object with some nonsense... temp++; } else { Qlist-&gt;SetName(names[count]); } workerfunc(lst, Qlist); // Pass to worker func count++; } } // shared_ptr goes out scope and I get ASSERT error from list!! } } void workerfunc(BaseHookQueueList* l, LIST item) // LIST is a typedef shared_ptr&lt;QueueList&gt; LIST { { boost::mutex::scoped_lock workerfunclock(listlock); l-&gt;push_front(*item); //Add to list } //Scope of lock { boost::mutex::scoped_lock workerfuncconsoleLock(consolelock); printf("Adding item to list...\n"); } ReleaseSemaphore(hConsumer, 1, NULL); // Set conumser Semaphore to Signalled with number of items currently on the queue } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload