Of course it causes an infinite loop !
You call the destructor, but the destructor also calls the destructor, so the destructor calls the destructor again… and again…
If you want to use delete
, you must use it from outside of the destructor and NOT call it again in the destructor.
To do that, you can use another static method which will mirror the GetInstance()
method :
class Singleton
{
public :
...
// this method is a mirror of GetInstance
static void ResetInstance()
{
delete m_pInstance; // REM : it works even if the pointer is NULL (does nothing then)
m_pInstance = NULL; // so GetInstance will still work.
}
...
~Singleton()
{
// do destructor stuff : free allocated resources if any.
...
}
Note : the other people warn you about using a singleton and they are right because this pattern is often misused. So think before using it. But go ahead anyway, that is the good way to learn !