SFML community forums
Help => Graphics => Topic started by: Dummie on March 17, 2008, 01:22:22 pm
-
Hey,
I'm currently trying to develop a ImageManager. Unfortunately I ran into a problem during developing it. My program crashs if I try to clean up the memory.
class ImageManager
{
typedef std::map<std::string, sf::Image*> imagemap_t;
imagemap_t images;
private:
ImageManager() {}
ImageManager(const ImageManager&);
~ImageManager() {
for (imagemap_t::iterator it = images.begin(); it != images.end(); it++) {
delete (*it).second; // !!!Crash!!!
}
}
public:
sf::Image& getImage(std::string const & fileName)
{
const imagemap_t::iterator it(images.find(fileName));
if (it != images.end()) return *it->second;
imagemap_t::iterator insertIt (images.insert(std::make_pair(fileName, new sf::Image())).first);
(*insertIt->second).LoadFromFile(fileName);
return *insertIt->second;
}
public:
inline static ImageManager& instance()
{
static ImageManager instance;
return instance;
}
};
I don't know why :( Is there any solution? Has anybody here wrote succesfully a ImageManager already? Wheres the problem in my code?
Thanks in advance :)
-
did you test, on which delete it crashes??
I did something very much the same in the "sone suggestions" post (though its totaly useless, since one can only import it to a singel file.. but the principle is the same^^) and make you imageManager a singleton (via static members..)
-
did you test, on which delete it crashes??
I did something very much the same in the "sone suggestions" post (though its totaly useless, since one can only import it to a singel file.. but the principle is the same^^) and make you imageManager a singleton (via static members..)
It does crash on the first call of delete. I also searched for the code you mentioned. I just found but yours isn't a class itself.
Maybe here has somebody a idea how to solve my problem. In my opinion a ImageManager or something similiar is even needed.
Thanks :)
-
I guess your ImageManager instance is destroyed after SFML has been unloaded. You should rather use a static pointer to be able to control its lifetime, and provide a Shutdown() function to delete it when appropriate (ie. before the global exit of your application).
-
Just curious, why set the instance function as inline?
-
I have a similar problem. My implementation of an image manager uses a std::map to ensure resources aren't loaded twice:
std::map<std::string, sf::Image> g_Image;
void CEntity::SetImage(const std::string &filename) {
std::map<std::string, sf::Image>::iterator it = g_Image.find(filename);
if (it == g_Image.end()) {
sf::Image image;
image.LoadFromFile(filename);
it = g_Image.insert(it, std::pair<std::string, sf::Image>(filename, image));
}
m_Sprite.SetImage(it->second);
}
The code works as intended when all entity objects are created before the sf::RenderWindow. However, I hope to create entity objects while in the main loop of my application (after the window has been created). The problem with that is SFML is unloading before the sf::Image destructors are executed. Can anyone tell me how to work around this? Thanks in advance!
Full source code available here (http://files.filefront.com/Tilezip/;9899538;/fileinfo.html).
-
The problem with that is SFML is unloading before the sf::Image destructors are executed
As I already said :
You should rather use a static pointer to be able to control its lifetime, and provide a Shutdown() function to delete it when appropriate (ie. before the global exit of your application).
-
Sorry, I figured that only applied to when using new/delete to allocate space. I'll have to research more on the subject of static pointers. If it's not too much trouble, could you possibly give an example of this?
-
An example of what ?
All you need to do is that :
int main()
{
... your game ...
g_Image.clear();
return 0;
}
-
Thanks a ton. I was doing it with new/delete because I got fed up with googling static pointers. This way looks better though, so I will implement it.
PS: Stop making it look so easy for us beginners! :wink:
-
PS: Stop making it look so easy for us beginners!
Ok, next time I'll try to provide a very complicated source code with a lot of templates and boost / STL stuff in it ;)