开发者

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. 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. Similarily, size() returns a non-zero result.

Info: I'm compiling in Visual Studios 2010 and linking against 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 somethi开发者_Go百科ng 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);

Here you are taking a pointer to a temporary object; note that is not even legal C++ but a nasty MSVC language extension. Right after you take a pointer to it, the temporary Sprite object is destroyed and you are left with an invalid pointer. You then derreference that pointer to get the id -which works just by chance here-, and then add that invalid pointer to the map. 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜