Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: ImageManager  (Read 5836 times)

0 Members and 1 Guest are viewing this topic.

Dummie

  • Newbie
  • *
  • Posts: 25
    • View Profile
ImageManager
« 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.

Code: [Select]

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 :)

tgm

  • Jr. Member
  • **
  • Posts: 69
    • View Profile
ImageManager
« Reply #1 on: March 17, 2008, 02:29:07 pm »
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..)

Dummie

  • Newbie
  • *
  • Posts: 25
    • View Profile
ImageManager
« Reply #2 on: March 17, 2008, 04:01:25 pm »
Quote from: "tgm"
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 :)

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
ImageManager
« Reply #3 on: March 17, 2008, 04:15:14 pm »
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).
Laurent Gomila - SFML developer

Kingdom of Fish

  • Jr. Member
  • **
  • Posts: 72
    • View Profile
ImageManager
« Reply #4 on: March 19, 2008, 02:23:50 am »
Just curious, why set the instance function as inline?

Wizzard

  • Full Member
  • ***
  • Posts: 213
    • View Profile
ImageManager
« Reply #5 on: March 27, 2008, 09:03:02 am »
I have a similar problem. My implementation of an image manager uses a std::map to ensure resources aren't loaded twice:

Code: [Select]
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.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
ImageManager
« Reply #6 on: March 27, 2008, 09:45:49 am »
Quote
The problem with that is SFML is unloading before the sf::Image destructors are executed

As I already said :
Quote
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).
Laurent Gomila - SFML developer

Wizzard

  • Full Member
  • ***
  • Posts: 213
    • View Profile
ImageManager
« Reply #7 on: March 27, 2008, 10:46:53 am »
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?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
ImageManager
« Reply #8 on: March 27, 2008, 11:41:11 am »
An example of what ?

All you need to do is that :
Code: [Select]
int main()
{
    ... your game ...

    g_Image.clear();
    return 0;
}
Laurent Gomila - SFML developer

Wizzard

  • Full Member
  • ***
  • Posts: 213
    • View Profile
ImageManager
« Reply #9 on: March 27, 2008, 11:42:16 pm »
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:

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
ImageManager
« Reply #10 on: March 28, 2008, 01:58:14 am »
Quote
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 ;)
Laurent Gomila - SFML developer

 

anything