我有以下代碼,它應該將線程A的shared_ptr實例添加到入侵鏈接列表中。其他消費者線程隨後會將其從名單。然而,在某個時候,我的shared_ptr似乎被破壞,並且在鏈接列表中對它的引用不再有效,導致Assert。shared_ptr在我可以正確使用前被破壞
有人能告訴我我做錯了什麼嗎?我懷疑這是因爲我在本地範圍內創建了原始的shared_ptr,並且它被破壞了。雖然名單應該仍然有一個參考? (這不是生產代碼)
編輯:
使用的變量Defintiions:
BaseHookQueueList* lst;
typedef boost::intrusive::list<QueueList> BaseHookQueueList;
class QueueList : public boost::intrusive::list_base_hook<>
// 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<QueueList> Qlist (new QueueList()); //Create shared_ptr instance
int temp = 0;
if (count > 5)
{
Qlist->SetName(names[temp]); // Fill up name property of QueueList object with some nonsense...
temp++;
}
else
{
Qlist->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<QueueList> LIST
{
{
boost::mutex::scoped_lock workerfunclock(listlock);
l->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
}
我猜'LIST'是一個typedef,但是是什麼?另外,什麼是'lst'? – ereOn 2010-10-07 12:41:36
typedef shared_ptr LIST; –
2010-10-07 12:48:22
發佈代碼時,如果您管理縮進以便大多數(如果不是全部)代碼和註釋都可以在不進行水平滾動的情況下看到,則會有所幫助。 – 2010-10-07 13:19:49