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 7541075
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T07:48:00+00:00 2026-05-30T07:48:00+00:00

gameObjects is a std::map<sf::String,VisibleGameObject*> , and results is a std::map<sf::String,VisibleGameObject*>::iterator . When this runs:

  • 0

gameObjects is a std::map<sf::String,VisibleGameObject*>, and results is a std::map<sf::String,VisibleGameObject*>::iterator. When this runs:

return gameObjects.erase(results);

I expected the destructor of VisibleGameObject to run, which is:

VisibleGameObject::~VisibleGameObject(){
    m_pSceneManager->removeSprite(name);
}

never runs, until the class which holds gameObjects is destroyed, which then runs:

GameObjectManager::~GameObjectManager(){
    std::for_each(gameObjects.begin(),gameObjects.end(),GameObjectDeallocator());
}

struct GameObjectDeallocator{
        void operator()(const std::pair<sf::String,VisibleGameObject*>&p) const{
            delete p.second;
        }
    };

then it does run. Why doesn’t it run in the first case?

Using SFML 2.0

Thanks

  • 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-05-30T07:48:02+00:00Added an answer on May 30, 2026 at 7:48 am

    erase removes the pointers from the container, but does not call delete.

    Suggestion:

    • change your map to simply be:

      std::map<sf::String,VisibleGameObject>
      

      i.e. objects not pointers to them

    or:

    • use a shared_ptr/unique_ptr (e.g. boost::shared_ptr or std::shared_ptr depending upon availability):

      std::map<sf::String,std::shared_ptr<VisibleGameObject> >
      

      which will call the destructor

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

Sidebar

Related Questions

I'm developing game. I store my game-objects in this map: std::map<std::string, Object*> mObjects; std::string
for(it = gameObjects.begin();it!=gameObjects.end();it++){ it->second->update(frameTime); if(it->second->getSprite()->GetPosition().y > 500){ std::cout << Removing enemy << std::endl; std::map<sf::String,VisibleGameObject*>::iterator
These lines of code are run: Enemy* enemy = new Enemy(m_pSceneManager,enemy+ss.str()); std::cout << Enemy
When I close my app in a certain state, this code runs: void GameEndState::exit(){
I work on a game project. Recently, we run into a problem which we
I have a C++ std::vector denoted as: std::vector<GameObject*> vectorToSort; Each object in vectorToSort contains
I realise this may be a duplicate, but I have searched through many forums
I am not sure how to approach this problem: 'Player' class maintains a list
Note that the order can go either way (erase first then push back, just
It seems the more I talk about this problem the better I understand it.

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.