Search code examples
c++visual-c++destructorstdmapdelete-operator

Deleting an std::map (Visual C++)


I have a pointer to a map that I am trying to delete (this map was allocated with new).

This map is valid I think, when I hover on it while debugging, it shows pMap: [0]() ..

When I try to delete this empty map, my app just quits and I get a

First-chance exception at 0xsomelocation in myapp.exe: 0xsomenumber: The object invoked has disconnected from its clients.

in the output window. What does this mean?

Thanks..

EDIT: Here's some sample code:

typedef map<const char*, StructA*, StructB> myMap;
typedef vector<myMap *> myMapStack;

StructB has an overloaded operator () Edit: StructB IS indeed a struct, sorry, the operator () is just a string comparing function..

In some part of my code, a class's constructor calls a method, let's call it InitClass(), that initializes a myMap pointer like so:

pMyMap = new myMap; // I also tried this with new myMap()
// this pointer is then pushed onto the a map stack
pMyMapStack.push_back(pMyMap);

Later on in this class' destructor, I go

pMyMap = pMyMapStack.back();
pMyMapStack.pop_back();

delete pMyMap; // after I step over this line the app quits.. and displays that message

Thanks

EDIT: I reverted back to an older version of the code that worked, and it's working fine now..

What worked was something like this:

// after the pMyMapStack.pop_back()
int x = pMyMap->size();
if (x >= 0)
    delete pMyMap;

Earlier on I had changed it to this:

// after the pMyMapStack.pop_back()
int (x = pMyMap->size();
if (x >= 0){
    pMyMap->clear();
    delete pMyMap;
}

Weird.. There might be something else wrong in the code, but I just can't figure out where yet.. It is too big (and I'd probably get fired) if I posted the code in it's entirety so let's just leave it at that..

I think it might have been a pointer to a null map that I was trying to clear or delete that was causing the problems..

Thanks for all those who tried to help... :)


Solution

  • honestly i think we are going no where without real code posted.

    there might be 101 place where the code went wrong, not limited to the snippet posted.

    from the object insertion and removal implementation shown, there are no syntax nor logical error. if the source code was so valuable to be shared on here, try create an dummy project, simple enough to demonstrate the problem (if the problem doesn't exist in dummy proj, you know you're tackling wrong direction)