Question about Singleton

I'm following the book SDL Game Development, here what I have in the class declaration:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class TextureManager
{
public:
	TextureManager(const TextureManager&) = delete;
	void operator=(const TextureManager&) = delete;
	~TextureManager();

	static TextureManager* Instance();
	
private:
	TextureManager();
	static TextureManager* pInstance;
	std::map<std::string, SDL_Texture*> textureMap;
};

TextureManager* TextureManager::pInstance = nullptr;


And here the Instance() method definition
1
2
3
4
5
6
7
8
TextureManager* TextureManager::Instance()
{
	if (nullptr == pInstance)
	{
		pInstance = new TextureManager;
	}
	return pInstance;
}


Am I doing this right?
Also, since this thing will be allocating texture, who's taking care of release it when the game end? In the book there is no mention to this.
Should I have a call like
 
delete TextureManager::Instance();

in the Game class destructor?
Last edited on
I prefer declaring the singleton instance as a static variable inside the function. That way it will automatically be destroyed at the end of the program.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Singleton
{
public:
	Singleton(const Singleton&) = delete;
	Singleton& operator=(const Singleton&) = delete;
	static Singleton& getInstance();
private:
	Singleton();
	~Singleton();
};

Singleton& Singleton::getInstance()
{
	static Singleton instance;
	return instance;
}


The book you are using might have the philosophy that it doesn't matter if not everything is freed at the end of the program. If the destructor doesn't do anything useful, such as writing to a file, it will work fine. The program might even close more quickly because it has less to do. The downside can be if you use a memory debugger, such as valgrind, it can be irritating to have to filter out all the memory leaks that doesn't matter to find the memory leaks that do matter.
Last edited on
I would like to declare it too as static inside the function, but then where does the
TextureManager* TextureManager::pInstance = nullptr; goes?
Is it fine where it is?
If instead I have
static TextureManager* pInstance = nullptr;
inside the function,
does it get initialized to nullptr only once,right? So should be fine?
Last edited on
I would like to declare it too as static inside the function, but then where does the
TextureManager* TextureManager::pInstance = nullptr; goes?

It goes away. You don't need it anymore.

If instead I have
static TextureManager* pInstance = nullptr;
inside the function,
does it get initialized to nullptr only once,right? So should be fine?

Yeah, it gets initialized to nullptr only once but it has the same problem as your original code in that the TextureManager is not automatically destroyed at the end of the program.
Last edited on
but it has the same problem as your original code in that the TextureManager is not automatically destroyed at the end of the program.


How come? You said
I prefer declaring the singleton instance as a static variable inside the function. That way it will automatically be destroyed at the end of the program


So the destructor should be called for TextureManager and everything should be cleared properly.
Am I missing something?
You probably missed that I didn't use a pointer in my example.
I totally missed on that detail xD

Thanks a lot Peter :)
Just to be sure, this is the way you do it right?

1
2
3
4
5
TextureManager* TextureManager::Instance()
{
	static TextureManager pInstance;
	return &pInstance;
}
As Peter87 indicated, you should return a reference rather than a pointer. With a pointer, the caller could do this:
1
2
3
TextureManager *i = TextureManager::Instance();
...
delete i;  // oops.  error 

or this:
1
2
3
4
5
6
7
TextureManager *i = nullptr;
...
if (somecondition) {
    i = TextureManager::Instance();
}
...
i->do_something();  // error. i can be nullptr 

If you return a reference, these bugs become much harder to create, so your code becomes more robust.
I also should look for a better book apparently, this SDL Game Development is teaching me how to do stuff in a bad way I think... x_x
Topic archived. No new replies allowed.