I have doubt in singleton design pattern. Basically Singleton is that there will be only one instance of the class being created for the whole application.
Sampletry getinstance initial minstance = (nil)
Sampletry minstance is null
[==========] Constructor Sampletry::Sampletry()
Sampletry getinstance after minstance = 0x636010
Sampletry::printfun printing
Sampletry maininstance = 0x636010
Sampletry maininstance as int = 6512656
.
I would like to know when will this memory be released ? i believe singleton instance will exists untill the program exits. so when this program exits , the destructor was not called and I believe this memory was not released.
Can someone explain this behaviour and when will this memory be released.
You normally have to terminate a singleton yourself.
I would make a private destructor and a public Terminate() function that calls it. Then manually call it before the program ends.
It means the programmer has to manually delete it, but it also means it can't accidentally be destroyed. If a programmer forgets to delete it, it's a similar situation to using the new keyword without using delete.
// implementation file: Sampletry.cc
namespace Sampletry
{
namespace
{
// what would have been nmember variables of a singleton object go here
// ...
}
void printfun()
{
// TODO:
}
};