Removing std :: map (Visual C ++)

I have a pointer to the card that I am trying to delete (this map was highlighted new).

This card is valid, I think when I find it during debugging, it shows pMap : [0]() ..

When I try to delete this blank card, my application just shuts down and I get

First chance exception in 0xsomelocation in myapp.exe: 0xsomenumber: the called object disconnected from its clients.

in the output window. What does it mean?

Thanks..

EDIT: Here is a sample code:

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

StructB has an overloaded operator () Edit: StructB IS really a structure, sorry, operator () is just a string comparison function.

In some part of my code, the class constructor calls a method, let it call InitClass (), which initializes the myMap pointer as follows:

 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 in the destructor of this class 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 went back to an older version of the code that worked, and now it works fine.

What worked was something like this:

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

I used to change it to this:

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

Strange .. There might be something else wrong with the code, but I just can't figure out where else .. It's too big (and I probably would have fired it) if I had posted the whole code, so let me just leave it at the same time.

I think it could be a pointer to a null card that I was trying to clear or delete, which caused problems.

Thanks to everyone who tried to help ... :)

+4
source share
6 answers

Honestly, I think that we will not go where the real code will not be sent.

there may be 101 places where the code did wrong, not limited to the published fragment.

There is no syntax or logical error from the presented implementation of inserting and deleting an object. if the source code was so valuable for sharing here, try creating a dummy project simple enough to demonstrate the problem (if the problem does not exist in the dummy project, you know you are solving the wrong direction)

+1
source

Well, there are a lot of problems with your sample code. When you create a map instance, you start to make mistakes: <const char*, StructA*, StructB*> Do you have a reason for storing pointers on the map instead of values? std::map will most likely save the items you add to it in the heap. Also you should use std::string instead of const char* . Then there is absolutely no reason to pass a pointer in the comparator.

Again, this is true for your mapStack (If you need a stack, why don't you use it?). As long as you do not share objects or use pure virtual base classes, it makes no sense to use pointers. And if you need to use pointers, try not to use raw pointers.

Once you have developed these errors, there should be no reason to use new or delete .

+5
source

Just in the dark, as there is no code. Are you sure that the pointer refers to one map, and not to an array of maps (in this case, you need to use delete [] )?

+1
source

You must do the deletion before you do pop_back ().

+1
source

You throw pointers like no tomorrow. The most likely explanation is that you clone your map structure by writing through the freed (but not null) pointer. See the answer "pmr" for more suggestions. Do not use pointers unless you need to. There is little reason in your code to deal with map pointers, not map objects.

+1
source

Since nothing is said about your code (which you posted) that might cause this problem, I Googled for your error line and found that it was something related to COM.

This post may help you: What could be the cause of error 0x80010108 (the called object has disconnected from its clients)?

Not relevant: I'm interested in:

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

As your third template parameter is a struct pointer ? I suggested that this should be a simple class / type.

+1
source

Source: https://habr.com/ru/post/1302716/


All Articles