I've a singleton class as follows:
class myClass
{
public:
static myClass* getInstance();
~myClass();
private:
static myClass* m_instance;
protected:
myClass();
};
and for above class definition is:
myClass* myClass::m_instance = 0;
myClass::myClass()
{
}
myClass::~myClass()
{
}
myClass* myClass::getInstance()
{
if(m_instance == 0)
m_instance = new myClass;
return m_instance;
}
As it's known, once memory is allocated with new
, it ought to be released to heap to prevent memory leak. In my case I have allocated memory which is not concerned with destructor due to that it's static.
So, how can I release memory allocated? Am I supposed to free it at all? Won't that lead to a memory leak as I have other classes' objects that function in the main()
as well?
PS: the object returned by getInstance()
method exists in the main()
until the shutdown of an application.