C++ Singleton design pattern

Posted by Artem Barger on Stack Overflow See other posts from Stack Overflow or by Artem Barger
Published on 2009-06-17T16:02:22Z Indexed on 2012/10/31 11:01 UTC
Read the original article Hit count: 169

Filed under:
|
|

Recently I've bumped into a realization/implementation of the Singleton design pattern for C++. It has looked like this (I have adopted it from the real life example):

// a lot of methods are omitted here
class Singleton
{
   public:
       static Singleton* getInstance( );
       ~Singleton( );
   private:
       Singleton( );
       static Singleton* instance;
};

From this declaration I can deduce that the instance field is initiated on the heap. That means there is a memory allocation. What is completely unclear for me is when exactly the memory is going to be deallocated? Or is there a bug and memory leak? It seems like there is a problem in the implementation.

My main question is, how do I implement it in the right way?

© Stack Overflow or respective owner

Related posts about c++

Related posts about design-patterns