pop_front crashing the program
I'm creating a pool of threads that cosumes a buffer and do some actions on my application. I've created a std::list m_buffer and sometimes the application crashes on the end of the buffer. Here's my code:
MyObject* myObject = 0; bool hasMore = true; while(hasMore) { { boost::unique_lock lck(m_loadMutex1); if(!m_buffer.size()) break; myObject = m_buffer.front(); m_buffer.pop_front(); hasMore = m_buffer.size(); } } if(myObject) loadMyObject(myObject);
I'm sure the list never starts empty. And a lot of threads executes this piece of code at the same time. And testing it on Windows sometimes the application crashes and the debugger says it was on pop_front. Bu开发者_JS百科t I can't believe its there because I check if the size is more than 0.
Thanks for helping.
Set first-chance exceptions in the debugger and see what the exception is that is being thrown.
I can't see a way that the code as posted would contain such a problem.
Most likely somewhere ELSE you're adding or removing from the list, unprotected, at the same time this function is popping.
Alternately there could be random memory corruption. Check the size of the buffer in the debugger when the pop_front
fails.
I would review how the program terminates. Maybe the list has been deallocated. This could happen in a variety if ways. For example: the list is created on the main thread, the main thread finishes and deallocates the list, and the other threads are still using the list. I mention this particular example because I do not see any logic in the thread method above that lets each thread know it is 'shutdown' time.
精彩评论