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

Author Topic: Using a singleton pattern for a resource manager.  (Read 3181 times)

0 Members and 1 Guest are viewing this topic.

piluve

  • Newbie
  • *
  • Posts: 7
    • View Profile
    • Email
Using a singleton pattern for a resource manager.
« 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!

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10849
    • View Profile
    • development blog
    • Email
Re: Using a singleton pattern for a resource manager.
« Reply #1 on: November 23, 2015, 12:55:22 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.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

piluve

  • Newbie
  • *
  • Posts: 7
    • View Profile
    • Email
Re: Using a singleton pattern for a resource manager.
« Reply #2 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!

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Using a singleton pattern for a resource manager.
« Reply #3 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.
Laurent Gomila - SFML developer

piluve

  • Newbie
  • *
  • Posts: 7
    • View Profile
    • Email
Re: Using a singleton pattern for a resource manager.
« Reply #4 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.

 

anything