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

Answers


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)


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.


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 [])?


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


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.


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.


Need Your Help

insert multiple rows in a saveall in cakephp

php cakephp cakephp-2.0 cakephp-2.1

i'm newbie in Cake and wodering how to insert multiple rows in a single saveall function,

With-binding not working for ko.computed()

javascript knockout.js

I´m struggling with a ko.computed-function ans the with binding.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.