mardi 27 février 2018

Accessing pointers after erasing from Map

The sample scenario in my code implementation is as follows

I have a map defined as map<int,map<int,object*>*> . The inner map which is in heap has a object pointer.

The scenario is,

After using(processing) all the elements in the inner map. I will erase the inner map contents using iterator. But the object* will not be deleted. I will use the object pointer further after erasing the key from the map.

My question is will that object* exist even after erasing it's presence in the map. As far as my understanding , yes the object is in heap and it can be used even after the erase in map. But i am facing random crash in the process after few minutes of execution. This makes me to post the question here.

multimap<ULONG, Class*>::iterator it_top3 = InnerMap->begin();
if (InnerMap->size() >= classLimit)
{
    if (it_top3->first <= ClassObj->m_classSize)
    {
        if (it_top3->second != NULL)
        {
            delete it_top3->second;
            it_top3->second = NULL;
        }
        InnerMap->erase(it_top3);
        InnerMap->insert(pair<ULONG, Class*>(ClassObj->m_classSize, ClassObj));
}

Secondly , On analyzing debug diag the line it_top3->second = NULL; points as the crash point with access violation exception. What would be possible reason for the crash here.?

Aucun commentaire:

Enregistrer un commentaire