clamp
clamp

Reputation: 34036

Why does my app crash at delete?

while(!m_RemoveNodeList.empty())
{
    list<CNode *>::const_iterator const it = m_RemoveNodeList.begin();

    CNode * const pNode = *it;
    ASSERT(pNode != NULL);

    m_NodeList.remove( pNode );

    delete pNode; // crashing here

    m_RemoveNodeList.pop_front();
}

The above sometimes crashes at the delete with a read violation exception. could i be that i accidentally double delete?

both m_NodeList and m_RemoveNodeList are of type

 std::list<CNode *>

i should mention that CNode is a base class for several other classes. however none of those classes does anything in their destructors

Upvotes: 2

Views: 998

Answers (2)

iammilind
iammilind

Reputation: 70088

There is no apparent crash in your code and looks fine.

It could crash only if there are duplicate CNode* are stored inside the list<CNode*>; which will lead you to multiple delete. (it's mentioned by @pau.estalella in the comments).

You can try following method to catch if there are duplicate CNode*.

map<CNode*, int> duplicate;
while(m_RemoveNodeList.size() > 0)
{
    list<CNode *>::const_iterator const it = m_RemoveNodeList.begin();
    CNode * const pNode = *it;
    if(duplicate.find(pNode) == duplicate.end())
      duplicate[pNode] = 1;
    else
      cout<<"Caught: "<<pNode<<endl;
// ...
}

Upvotes: 3

pNode is just a reference to the original, not a copy. Not sure of what remove does, but if does indeed the original, you have a double delete.

Upvotes: 0

Related Questions