SFML community forums

Help => General => Topic started by: piluve on November 23, 2015, 12:16:02 pm

Title: Using a singleton pattern for a resource manager.
Post by: piluve on November 23, 2015, 12:16:02 pm
Before getting to the question let me explain my problem.

I´ve made a simple ResourceManager where I could add/get resources (sf::Textures,sf::Font etc) and I made it static inside a "Types" file (yeah , you start seeing the problem :D )

When I close the app it will crash (ntdll exception), that confused me a lot.I put down a few breakpoint and the exception was outside of my app.
So I searched google for an answer and I found that I can not use static global things with SFML because we dont know when they will be destroyed and that will cause a few problems.


After this small introduction ;) , now the question.

How do I make a static/global class?

Can I use a singleton pattern like this:

http://www.yolinux.com/TUTORIALS/C++Singleton.html
#include <string>

class Logger{
public:
   static Logger* Instance();
   bool openLogFile(std::string logFile);
   void writeToLogFile();
   bool closeLogFile();

private:
   Logger(){};  // Private so that it can  not be called
   Logger(Logger const&){};             // copy constructor is private
   Logger& operator=(Logger const&){};  // assignment operator is private
   static Logger* m_pInstance;
};

That pattern holds the class as a static pointer, will be this a problem?

Thank you guys!
Title: Re: Using a singleton pattern for a resource manager.
Post by: eXpl0it3r on November 23, 2015, 12:55:22 pm
Your options are:

I suggest to not use global classes, it makes a lot harder to reason about your application. Instead just pass around references to your resource manager and reduce the points where its used.
Title: Re: Using a singleton pattern for a resource manager.
Post by: piluve on November 23, 2015, 01:17:58 pm
Your options are:
  • Don't use global classes
  • Don't instantiate classes in the global scope

I suggest to not use global classes, it makes a lot harder to reason about your application. Instead just pass around references to your resource manager and reduce the points where its used.

Yeah I tried to reference it to the objects that need to use it but at the end it goes really deep and it is just a nightmare.
Then the singleton will work right? As it just holds a pointer.
See you!
Title: Re: Using a singleton pattern for a resource manager.
Post by: Laurent on November 23, 2015, 01:43:26 pm
A compromise is to keep a global access to the instance, but creation/destruction under control in the main(). Because when you think about it, all you need is a globally accessible entry-point to your logger. You don't need all this singleton stuff, and can perfectly control when the instance is created and destroyed.
Title: Re: Using a singleton pattern for a resource manager.
Post by: piluve on November 23, 2015, 03:09:57 pm
Thanks for your time and help guys, I ended up encapsulating it into a singleton, I think its cleaner than having to reference in around the code.

See you.