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
eraseremoves the pointers from the container, but does not calldelete.Suggestion:
change your map to simply be:
i.e. objects not pointers to them
or:
use a
shared_ptr/unique_ptr (e.g.boost::shared_ptrorstd::shared_ptrdepending upon availability):which will call the destructor