Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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... :)

like image 716
krebstar Avatar asked Dec 30 '22 00:12

krebstar


2 Answers

Well, there are a lot of problems with your sample code. Things start to go awry when you instantiate the map as: <const char*, StructA*, StructB*> Is there a reason for you to store pointers in your map instead of values? std::map is likely to store the elements you add to it on the heap anyway. Also you should use std::string instead of const char*. Then there is absolutely no reason to pass in the comparator as a pointer.

Again this is true for your mapStack (If you need a stack, why don't you use one?). As long as you aren't sharing objects or use pure-virtual base classes there is no reason to use pointers. And if you have to use pointers, try to don't use raw pointers.

After you have worked out those errors there shouldn't be any reason for you do use new or delete.

like image 117
pmr Avatar answered Jan 09 '23 19:01

pmr


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)

like image 36
YeenFei Avatar answered Jan 09 '23 18:01

YeenFei