It's me and my BlockingQueue again... I rewrote it according to this article and this question. It sends some items and then crashes with an access violation. Here's the code:
template <typename T>
bool DRA::CommonCpp::CTBlockingQueue<T>::Push( T pNewValue ){
volatile long oldSize;
::InterlockedExchange( &oldSize, m_Size );
CTNode* pNewNode = new CTNode();
pNewNode->m_pValue = pNewValue;
{//RAII block
CGuard g( m_TailCriticalSection );
m_pTailNode->m_pNext = pNewNode;
m_pTailNode = pNewNode;
::InterlockedIncrement( &m_Size );
}
if( oldSize == 0 )
m_eAtLeastOneElement.set();
return true;
}
template <typename T>
bool DRA::CommonCpp::CTBlockingQueue<T>::Pop( T& pValue ){
CTNode* pCurrentNode;
{//RAII block
CGuard g( m_HeadCriticalSection );
pCurrentNode = m_pHeadNode;
CTNode* pNewHeadNode = m_pHeadNode->m_pNext;
if( pNewHeadNode == NULL ){
CEvent* pSignaledEvent;
CEvent::waitForPair( m_eAtLeastOneElement, m_eFinished, pSignaledEvent );
if( pSignaledEvent == &m_eFinished )
return false;
pNewHeadNode = m_pHeadNode->m_pNext;
}
pValue = pNewHeadNode->m_pValue;
m_pHeadNode = pNewHeadNode;
::InterlockedDecrement( &m_Size );
}
delete pCurrentNode;
return true;
}
It always crashes in a call to Pop(), in the line after the if, the one which says:
pValue = pNewHeadNode->m_pValue
It blows up cos' pNewHeadNode is NULL. But how can this happen?
Edit: Forgot initialization code:
template <typename T>
DRA::CommonCpp::CTBlockingQueue<T>::CTBlockingQueue():
m_HeadCriticalSection("CTBlockingQueue<T>::m_Head"),
m_TailCriticalSection("CTBlockingQueue<T>::m_Tail"){
CTNode* pDummyNode = new CTNode();
m_pHeadNode = pDummyNode;
m_pTailNode = pDummyNode;
m_Size = 0; //Dummy node doesn't count
}