Search code examples
c++visual-studio-2010c++11shared-ptrsfml

std::shared_ptr _BLOCK_TYPE_IS_VALID(pHead->nBlockUse) when changing pointer


When this function runs, I get the debug assertion error as described in the title on the line sprite = spr;. If I add sprite.reset(); before that, it crashes on the line with sprite.reset();. The pointer is being stored elsewhere, in static std::map<std::string,sf::Sprite> ResourceManager::sprites;, so I would not expect the destructor to be called for the sf::Sprite either (although I have a suspicion it may be because they are being stored in a static object...?)

VisibleGameObject.h

#ifndef VISIBLEGAMEOBJECT_H
#define VISIBLEGAMEOBJECT_H

#include "Game.h"

//just for keeping track of the sprite and drawing

class VisibleGameObject{
public:
    VisibleGameObject(){}
    VisibleGameObject(const std::string& name);
    ~VisibleGameObject();

    std::string getTextureName();
    void setSprite(const std::string& textureName);
    void setSprite(const sf::Texture& texture);
    void setSprite(std::shared_ptr<sf::Sprite> sprite);

    void setPosition(float x,float y);
    void setPosition(const sf::Vector2f& position);

    void setRotationDegrees(float degrees);
    void setRotationRadians(float radians);
    float getRotationDegrees();
    float getRotationRadians();

    void setOrigin(float x,float y);
    void setOrigin(const sf::Vector2f& origin);

    sf::Vector2f getSize();
    sf::Vector2f getOrigin();
    sf::Vector2f getPosition();
    std::shared_ptr<sf::Sprite> getSprite();

    void draw(tgui::Window* wnd);
private:
    std::string name;
    std::shared_ptr<sf::Sprite> sprite;
    std::string texture_name;
    bool _loaded;
};

#endif

Extract from VisibleGameObject.cpp

//'sprite' is initialised here
    void VisibleGameObject::setSprite(const std::string& textureName){  
        sprite = std::shared_ptr<sf::Sprite>(ResourceManager::createSpriteFromStoredTexture(textureName,name));
        texture_name = textureName;
        _loaded = true;
    }


//error function!
void VisibleGameObject::setSprite(std::shared_ptr<sf::Sprite> spr){
    sf::Vector2f p(0,0);
    float d = 0;
    if(_loaded){
        p = spr->getPosition();
        d = spr->getRotation();
    }
    sprite = spr;
    sprite->setPosition(p);
    sprite->setRotation(d);
    _loaded = true;
}

Extract from ResourceManager.cpp

sf::Sprite* ResourceManager::createSpriteFromStoredTexture(const std::string& texturename,const std::string& spritename){
    sf::Sprite spt;
    spt.setTexture(*getTextureByName(texturename));
    std::string name = spritename;
    if(spritename == standard_spt_name){
        name = spritename+std::to_string((long long)spritecount);
        spritecount++;
    }

    sprites[name] = spt;
    return &sprites[name];
}

The VisibleGameObject appears to function correctly when being used without changing the sprite with the setSprite function originally described as the problem.


Solution

  • You're failing because you're creating a std::map<std::string,sf::Sprite>, which actually owns the 'sf::Sprite' object. You're then giving that a pointer to the object that's IN the map to a std::shared_ptr<sf::Sprite>, and using the default deleter.

    This means when the last reference to the shared pointer goes out of scope, it will call delete on the object. However that object is part of the map. This causes undefined behavior (and in this case an assertion).

    There are a few possible solutions:

    1) Don't use shared_ptr in this case. Since the ownership of the sf::Sprite is always with the map, then you can simply not bother with the shared_ptr and instead use a plain pointer.

    2) Use a custom deleter that does nothing. If you're interested in hiding the fact that the sf::Sprite is owned by the map (lets say in some cases you want to create it on the fly), then, you need to create a null-deletion function. Here I'm using a lambda, but you could create your own function. This causes the shared_ptr to do nothing when the last reference goes out of scope. Since the memory isn't really owned by the shared_ptr, this is the behavior you want.

    sprite = std::shared_ptr<sf::Sprite>(ResourceManager::createSpriteFromStoredTexture(textureName,name), [](const void*){} );
    

    Edit:

    Actually, I would go with a modification of the second option. Rather than return a raw pointer, I would have the createSpriteFromStoredTexture method return the shared_ptr, and then use the noop deleter in there. That way, the user of the function is not aware of how the sprite shared_ptr is created, it simply knows that it has a shared_ptr at this time.

    3) Use a map of shared_ptr<sf::Sprite>. The map will always own the sprites, but it makes the use of the shared_ptr a bit more natural.