Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8442533
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T08:52:06+00:00 2026-06-10T08:52:06+00:00

When this function runs, I get the debug assertion error as described in the

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T08:52:07+00:00Added an answer on June 10, 2026 at 8:52 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Maddening problem here. When my page loads: <body onload=getClientDateTime();> It runs this function: document.getElementById('ClientDateTime').value=hello
I have this function that takes the input of a, runs a calculation and
Linux bash script: function Print() { echo $1 } Print OK This script runs
This function works fine: std::string get_str() { return std::get<0>( make_tuple(std::string(hi)) ); } But if
This function gives error: function RefreshGrid(){ window.location.href=Form_ElameMamoreBazdid.aspx; }
This macro runs on the click of a button. I receive an error. Run-time
I'm trying this: function send_sms() { $liveQuery = $this->db->get('liveList'); $counter = 0; foreach($liveQuery->result() as
I have this function: /*This func runs *.c1 file, and replace every include file
This function is in a Class file in App_Code folder Public Shared Function CRUD(ByVal
This function works as it should, window.setInterval(function(){ var active = $('#frontpageControls a.active'); var next

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.