title: Memory leak in singleton class in c++
body: <p>i have a singleton class. I am creating an object of it. Using it. Once it will go out of scope destructor will be called.</p>
  
  <p>Again creating anew object,  since instanceFlag is false, it will again allocate new memory.</p>
  
  <pre><code>#include &lt;iostream&gt;
  
  using namespace std;
  
  class Singleton
  {
  private:
      static bool instanceFlag;
      static Singleton *single;
      Singleton()
      {
          //private constructor
      }
  public:
      static Singleton* getInstance()
      {
          if(! instanceFlag)
          {
              single = new Singleton();
              instanceFlag = true;
              return single;
          }
          else
          {
              return single;
          }
      }
      void method()
      {
          cout &lt;&lt; "Method of the singleton class" &lt;&lt; endl;
      }
      ~Singleton()
      {
          instanceFlag = false;
      }
  };
  
  bool Singleton::instanceFlag = false;
  Singleton* Singleton::single = NULL;
  
  
  
  int main()
  {
      Singleton *sc1,*sc2;
      {
          sc1 = Singleton::getInstance();
          sc1-&gt;method();
          delete sc1;
      }
      sc2 = Singleton::getInstance();
      sc2-&gt;method();
  
      return 0;
  }
  </code></pre>
  
  <p>My doubt is what will happen to old memory? I think its a memory leak. If yes how to fix this issue in the given code?</p>
  
  <p>Any comments will be helpful to understand the internals.</p>