2

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.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
elgolondrino
  • 665
  • 9
  • 33
  • Or you can register a function to the `atexit()` function. – Uchia Itachi Aug 27 '13 at 05:47
  • 1
    Assuming you are running on an OS that provides a separate memory space for each process (i.e. just about any modern OS these days), the "memory leak" in this scenario is inconsequential, because immediately after main() returns, all memory that was allocated by the process is reclaimed by the OS anyway. The only time it might make a difference is if ~myClass() needs to do some cleanup work outside of the process space (e.g. delete a file from /tmp or something). Assuming that's not the case, the easiest solution is just to ignore the issue, as it won't cause you any problems. – Jeremy Friesner Aug 27 '13 at 06:06

3 Answers3

1

You could use a smart pointer instead of a raw pointer and then you don't have to think about it :)

If you are using C++11 you can use unique_ptr. If you are using an older compiler than use auto_ptr.

Also, the code above is not thread safe.

pstrjds
  • 16,840
  • 6
  • 52
  • 61
1

If the object persists until program shutdown, you don't necessarily have to free it. However, you may want to release resources (files, sockets, database connections, etc.) held by the singleton at shutdown time; you can do this with a static "deinitialization" function called during normal program shutdown.

By the way, the way you initialize the singleton is not threadsafe. You may want to use a threadsafe singleton instead.

Community
  • 1
  • 1
nneonneo
  • 171,345
  • 36
  • 312
  • 383
1

In this case you would have to delete your object using the pointer in your main (or wherever you have access to it before it goes out of scope).

Something like:

int main()
{
    myClass* inst = myClass::getInstance();
    // ... do something with it here
    delete inst;
}

Although this is generally a bad practice, because you should not have a new/delete "running wild" anywhere in your program. It's better to stick to the RAII concept and bind your heap-stored memory with some objects on stack using constructors and destructors.

In this specific case - instead of using "new mClass" in "getInstance()" you could just declare m_instance as static.

myClass& myClass::getInstance()
{
    static myClass instance;
    return instance;
}

This way you don't have to release any memory at all since it will be auto released with all other statics in your program.