views:

431

answers:

6

Hi,

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

+1  A: 

Just a shot in the dark, since no code is available. Are you sure the pointer is to a single map and not an array of maps (in which case you need to use delete [])?

Ari
Thanks, I think it is just a single map..
krebstar
+5  A: 

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.

pmr
Concur mostly, but to nitpick: pointers are useful not only if objects are shared, but also if subclassing is used. Which does not seem pertinent since we're talking about structs.
Pontus Gagge
@Pontus: Agreed and edited.
pmr
Sorry, this whole thing is actually quite big and complex and I am afraid of breaking things if I change stuff around a bit too much.. Let me see if I can find out more stuff... Thanks..
krebstar
+1  A: 

You should do the delete before you do the pop_back().

Adhemar
Technically invalid, but it doesn't matter. Even if it doesn't break additional things, the proposed fix won't remove the real bug.
MSalters
How is it invalid?
Adhemar
I wouldn't say it's invalid but it won't fix anything.
CiscoIPPhone
pop_back() calls the destructor on the element, so it seems like a bad idea to me to call delete on it afterwards. Obviously there are many other things wrong with the code, as pointed out by pmr.
Adhemar
pop_back() doesn't call the destructor, because the stack contains pointers to the elements, not the elements themselves.
CiscoIPPhone
I meant in general. Yes, in this case the value is a pointer, so there's nothing to destruct, but the memory could still be reused, no? Right now there's no code in between, but maybe he just didn't post it.
Adhemar
I'm not sure having a stack containing a dangling pointer is a better idea. This way his pointer is valid between the popping and the delete, so I don't understand what you are trying to say.
CiscoIPPhone
+1  A: 

You're throwing pointers around like there's no tomorrow. The most likely explanation is that you are clobbering your map structure by writing through a deallocated (but non-nulled) pointer. See pmr's answer for more suggestions. Don't use pointers unless you have to. There's little reason in your code to deal with map pointers rather than map objects.

Pontus Gagge
Say, if I really had to use the pointers, could I just make sure that every deallocated pointer is NULLED?
krebstar
+1  A: 

Since there's nothing about your code (that you posted) that could cause this problem I Googled for your error string and found it's something to do with COM.

This post could help you: http://stackoverflow.com/questions/2346728/what-may-be-the-causes-of-the-error-0x80010108-the-object-invoked-has-disconnect

Not relevant to the question: I find this interesting:

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

How is your third template parameter a struct pointer? I assumed it would have to be a plain class/type.

CiscoIPPhone
Thanks, it was a typo.. fixed..
krebstar
+1  A: 

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)

YeenFei
Thanks, I think it might be just that.. I need to do some more digging.. Thanks..
krebstar