简体   繁体   English

在空白地图上调用clear时崩溃

[英]Crash when calling clear on an Empty Map

I'm getting a strange crash when I try to interact with a map in this particular class. 当我尝试与此特定类中的地图进行交互时,我遇到了奇怪的崩溃。 When I try to call clear() or even begin() on the map it crashes. 当我尝试在地图上调用clear()甚至begin()时,它崩溃了。 I haven't added anything to map, at this point nothing has touched it at all. 我没有在地图上添加任何内容,这时一点都没有触及。

Code in question: 有问题的代码:

spriteMap_.clear();
if (!spriteMap_.empty())
{
    SpriteMap::const_iterator end = spriteMap_.end();
    for (SpriteMap::const_iterator it = spriteMap_.begin(); it != end; ++it)
    {
        it->second->draw(screen);
    }
}

Even stranger is that this is not unique to the map but to any maps in this particular class. 甚至更奇怪的是,这并非地图独有,而是此特定类中的任何地图独有。 I have another map, that is also not being touched until here (I tested it with a clear call in this function). 我有另一张地图,直到这里也没有被触及过(我在此函数中使用了清晰的调用对其进行了测试)。

When I use intelli-sense on the maps both show themselves with tons of values already in them and the empty() call returns false. 当我在地图上使用智能感知时,两个地图上都已经显示了大量的值,并且empty()调用返回false。 Similarily, size() returns a non-zero result. 同样,size()返回非零结果。

Info: I'm compiling in Visual Studios 2010 and linking against SDL. 信息:我正在Visual Studios 2010中进行编译,并针对SDL进行链接。

Any help is appreciate. 任何帮助表示赞赏。

Edit (more info): 编辑(更多信息):

My header has this line: 我的标题有这一行:

private: std::map spriteMap_;

And the only code that is hit is the function I showed you. 唯一碰到的代码就是我向您展示的功能。 I have other code but the break point on the function is never hit (I don't call that function). 我有其他代码,但是该函数的断点从未命中(我不调用该函数)。

But here it is: 但是这里是:

Sprite* SpriteManager::createSprite(std::string fileName)
{
   ...

   Sprite* newSprite = &Sprite(nextSpriteId_, this, image);
   nextSpriteId_++;
   spriteMap_[newSprite->id_] = newSprite;
   return newSprite;
}

Fixed: 固定:

Moral of the story is don't ever do something like this: 这个故事的寓意是永远不要做这样的事情:

ObjPtr* objPtr = &Obj();

You are probably corrupting the map´s memory somewhere within that class. 您可能正在破坏该类中某处的地图内存。 Get the map memory address, and see what happens when you attach breakpoints to such locations. 获取映射内存地址,并查看将断点附加到此类位置时会发生什么。

Edit: 编辑:

Sprite* newSprite = &Sprite(nextSpriteId_, this, image); Sprite * newSprite =&Sprite(nextSpriteId_,this,image);

Here you are taking a pointer to a temporary object; 在这里,您正在指向临时对象的指针; note that is not even legal C++ but a nasty MSVC language extension. 请注意,这甚至不是合法的C ++,而是讨厌的MSVC语言扩展。 Right after you take a pointer to it, the temporary Sprite object is destroyed and you are left with an invalid pointer. 在使用指向它的指针之后,临时Sprite对象被破坏,并且留下无效的指针。 You then derreference that pointer to get the id -which works just by chance here-, and then add that invalid pointer to the map. 然后,您取消引用该指针以获取id(在这里偶然起作用),然后将该无效指针添加到地图。 That´s at least one of the problems, it may or may not be related to your crash, there may be more problems out there. 那是问题中的至少一个,它可能与您的崩溃有关,也可能与您的崩溃无关,那里可能还有更多问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM