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
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.
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.
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?
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.
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?