删除 std::map (Visual C++)

发布于 2024-08-23 16:23:32 字数 1462 浏览 14 评论 0原文

我有一个指向我试图删除的地图的指针(该地图已分配新的)。

我认为这张地图是有效的,当我在调试时将鼠标悬停在它上面时,它显示 pMap: [0]() ..

当我尝试删除这个空地图时,我的应用程序刚刚退出,我得到一个

myapp.exe 中 0xsomelocation 处的首次机会异常:0xsomenumber:调用的对象已与客户端断开连接。

在输出窗口中。这意味着什么?

谢谢..

编辑:这是一些示例代码:

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

StructB 有一个重载运算符 () 编辑:StructB 确实是一个结构体,抱歉,运算符 () 只是一个字符串比较函数。

在我的代码的某些部分,类的构造函数调用一个方法,我们称之为 InitClass(),它初始化一个 myMap 指针,如下所示:

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

稍后在这个类的析构函数中,我去

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

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

谢谢

编辑:我恢复到可以工作的旧版本代码,现在工作正常..

有效的是这样的:

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

早些时候我已经将其更改为这样:

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

奇怪.. 代码中可能还有其他错误,但我只是不知道在哪里.. 如果我完整地发布代码,它太大了(而且我可能会被解雇),所以让我们离开在那..

我认为它可能是一个指向空映射的指针,我试图清除或删除它导致了问题..

感谢所有试图提供帮助的人...:)

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

我是有多爱你 2024-08-30 16:23:32

嗯,你的示例代码有很多问题。当您将映射实例化为: 时,事情就开始出错了
您是否有理由在映射中存储指针而不是值?
无论如何,std::map 可能会将您添加到其中的元素存储在堆上。另外,您应该使用 std::string 而不是 const char*。那么绝对没有理由将比较器作为指针传入。

同样,这对于您的 mapStack 来说也是如此(如果您需要一个堆栈,为什么不使用一个呢?)。只要您不共享对象或使用纯虚拟基类,就没有理由使用指针。如果必须使用指针,请尽量不要使用原始指针。

解决了这些错误后,您就没有任何理由使用 newdelete

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.

森林散布 2024-08-30 16:23:32

老实说,我认为如果没有发布真正的代码,我们就不会去任何地方。

代码可能有 101 个地方出错,不限于发布的代码片段。

从所示的对象插入和删除实现来看,没有语法或逻辑错误。
如果源代码非常有价值,值得在这里分享,请尝试创建一个虚拟项目,足够简单以演示问题(如果问题不存在于虚拟项目中,您就知道您正在解决错误的方向)

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)

友谊不毕业 2024-08-30 16:23:32

只是在黑暗中拍摄,因为没有可用的代码。您确定指针指向单个地图而不是地图数组(在这种情况下您需要使用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 [])?

忆离笙 2024-08-30 16:23:32

您应该在执行 pop_back() 之前执行删除操作。

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

云雾 2024-08-30 16:23:32

你到处乱扔指针,就像没有明天一样。最可能的解释是,您通过写入已释放(但非空)指针来破坏映射结构。更多建议请参阅 pmr 的回答。除非必要,否则不要使用指针。您的代码中没有理由处理映射指针而不是映射对象。

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.

美男兮 2024-08-30 16:23:32

由于您的代码(您发布的)没有任何内容可能导致此问题,因此我在 Google 上搜索了您的错误字符串,发现它与 COM 有关。

这篇文章可以帮助您:
可能是什么错误 0x80010108 的原因(调用的对象已与其客户端断开连接)?

与问题无关:
我觉得这很有趣:

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

你的第三个模板参数如何是结构指针?我认为它必须是一个普通的类/类型。

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:
What may be the causes of the error 0x80010108 (The object invoked has disconnected from its clients)?

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文