when is the singleton instance deleted ?

Mar 23, 2012 at 8:55am
Hi ,

I have doubt in singleton design pattern. Basically Singleton is that there will be only one instance of the class being created for the whole application.

I have a class as follows:

1
2
3
4
5
6
7
8
9
10
11
12
13
Sampletry.h
class Sampletry 
{
public:
	static Sampletry * getinstance();
	static Sampletry *minstance;
	virtual ~Sampletry();
	void printfun(){ printf("\n Sampletry::printfun printing  \n"); };
	
protected:
	Sampletry();

};


Sampletry.cpp
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
#include "Sampletry.h"
#include <stdio.h>

int main(int argc, char **argv)
{
	Sampletry *maininstance = Sampletry::getinstance();

	maininstance->printfun();
	printf("\n Sampletry maininstance = %p \n",maininstance);
	printf("\n Sampletry maininstance as int = %d \n",maininstance);

	return 0;
}

Sampletry *Sampletry::minstance = NULL;

Sampletry* Sampletry::getinstance()
{
	printf("\n Sampletry getinstance initial minstance = %p \n",minstance);
	if( minstance == NULL)
	{
		printf("\n Sampletry minstance is null \n");
		minstance = new Sampletry();
		printf("\n Sampletry getinstance after minstance = %p \n",minstance);
	}
    return minstance;
}

Sampletry::Sampletry()
{
	printf("[==========] Constructor Sampletry::Sampletry() \n");
}

Sampletry::~Sampletry()
{
	printf("[==========] destcutor Sampletry::~Sampletry() \n");
}


The following is the program outpout:
 Sampletry getinstance initial minstance = (nil) 

 Sampletry minstance is null 
[==========] Constructor Sampletry::Sampletry() 

 Sampletry getinstance after minstance = 0x636010 

 Sampletry::printfun printing  

 Sampletry maininstance = 0x636010 

 Sampletry maininstance as int = 6512656
.


I would like to know when will this memory be released ? i believe singleton instance will exists untill the program exits. so when this program exits , the destructor was not called and I believe this memory was not released.

Can someone explain this behaviour and when will this memory be released.
Mar 23, 2012 at 9:08am
You normally have to terminate a singleton yourself.

I would make a private destructor and a public Terminate() function that calls it. Then manually call it before the program ends.

It means the programmer has to manually delete it, but it also means it can't accidentally be destroyed. If a programmer forgets to delete it, it's a similar situation to using the new keyword without using delete.

The only workaround I can think of is to use smart pointers: http://en.wikipedia.org/wiki/Smart_pointer
Mar 23, 2012 at 10:21am
Create the instance as a static variable inside the getinstance function. That way it will automatically be destructed when the program terminates
1
2
3
4
5
Sampletry& Sampletry::getinstance()
{
    static Sampletry minstance;
    return minstance;
}

Last edited on Mar 23, 2012 at 10:22am
Mar 23, 2012 at 2:27pm
Why do you need a singleton object for this? C++ has namespaces.

1
2
3
4
5
// header: Sampletry.h
namespace Sampletry 
{
    void printfun() ;
};

1
2
3
4
5
6
7
8
9
10
11
12
13
14
// implementation file: Sampletry.cc
namespace Sampletry 
{
    namespace 
    {
       // what would have been nmember variables of a singleton object go here
       // ...
    }
    
    void printfun()
    {
        // TODO:
    }
};

1
2
3
4
int main()
{
    Sampletry::printfun() ;
}

Topic archived. No new replies allowed.