So I've written a simple singleton class. When I create an object the constructor is called, but it's destructor (which frees the object) doesn't seem to be called when it goes out of scope.
#include <iostream>
using namespace std;
class Singleton {
public:
static Singleton &getInstance( )
{
if (instance == nullptr) {
cout << "Creating instance.\n";
instance = new Singleton();
}
return *instance;
}
static void destroyInstance( )
{
if (instance != nullptr) {
cout << "Destroying instance.\n";
delete instance;
instance = nullptr;
}
}
~Singleton( )
{
if (instance != nullptr) {
cout << "Destroying instance.\n";
delete instance;
instance = nullptr;
}
}
private:
Singleton( ) { }
static Singleton *instance;
};
Singleton *Singleton::instance = nullptr;
int main( )
{
Singleton &singleton = Singleton::getInstance();
//singleton.destroyInstance();
return 0;
}
With the destructor code the program just outputs this.
Creating instance.
If I comment out the destructor and use the destroyInstance()
function it outputs this.
Creating instance.
Destorying instance.
Why is it doing this?
EDIT: I'm an idiot. Just overlooked a bunch of things. This question might as well be deleted as it's not really that useful.