Singleton class memory management

I have a question about the singleton class. The following is a typical application singleton c++ application.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <iostream>

using namespace std;

class Singleton
{
private:
    static bool instanceFlag;
    static Singleton *single;
    Singleton()
    {
        //private constructor
    }
public:
    static Singleton* getInstance();
    void method();
    ~Singleton()
    {
        instanceFlag = false;
    }
};

bool Singleton::instanceFlag = false;
Singleton* Singleton::single = NULL;
Singleton* Singleton::getInstance()
{
    if(! instanceFlag)
    {
        single = new Singleton();
        instanceFlag = true;
        return single;
    }
    else
    {
        return single;
    }
}

void Singleton::method()
{
    cout << "Method of the singleton class" << endl;
}

int main()
{
    Singleton *sc1,*sc2;
    sc1 = Singleton::getInstance();
    sc1->method();
    sc2 = Singleton::getInstance();
    sc2->method();

    return 0;
}


My question is when the heap is allocated using: single = new Singleton(), where does the heap get releaed? Should we add delete single in destructor ~Singleton(), please help.
It does not get deallocated ever.

Certainly you cannot free it in the destructor for two reasons: 1) the destructor will never run, and 2) if it did, you'd end up with an infinite loop.

If you want the object's destructor to run when the program exits, then implement it this way:

1
2
3
4
5
6
7
8
9
10
class Singleton {
   Singleton() {}
   Singleton& operator=( const Singleton& );  // Leave unimplemented
   Singleton( const Singleton& );  // Leave unimplemented
  public:
    static Singleton& GetInstance() {
        static Singleton singleton;
        return singleton;
    }
};


(Note: your implementation of the Singleton pattern was incomplete in
that it allowed copy constructor and assignment, both of which break
the singleton pattern. My code above also fixes that).
Thanks a lot.
It's easy to free memory

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Singleton
{
public:
//here all your staff
//please don't call this method, it was called automatically
static void terminate()
{
    delete single;
    single = 0;
}
};

class SingletonDeletor
{
public:
    ~SingletonDeletor()
      {
           Singleton::terminate();
      }
};


//in some cpp file you need to declare static variable of SingletonDeletor
static SingletonDeletor sSingletonDeletor;
Last edited on
And once more comment, if your global or static variables are used this singleton then my solution is not acceptable
Last edited on
jsmith's solution is what I suggest. It doesn't instantiate the instance until you use it and it cleans it up when the program exits. What's not to like about that?
There is a small difference. It's memory usage, in my case I use heap, in jsmith's case data segment is used. The difference may be not sensitive for desktops, but for embedded systems it's prefer to use heap.
Topic archived. No new replies allowed.