Singleton Class

K

Kumar Saurabh

Hi,

I am using a singleton class like the one given below:

class singleton
{
static singleton *object;
singleton();

public:
getInstance();
~singleton();
};


From some other function i try to do the following:

singleton *p= singleton :: getInstance();
detele p;


But here my program dumps. What could be the problem.

Thanks and Regards,
Saurabh
 
B

Ben Pope

Kumar said:
Hi,

I am using a singleton class like the one given below:

class singleton
{
static singleton *object;
// Where is the singleton allocated?
singleton();

public:
getInstance();

// What does this return?
~singleton();
};


From some other function i try to do the following:

singleton *p= singleton :: getInstance();
detele p;

The singleton object should deal with it's own lifetime, why are you
trying to delete it?
But here my program dumps. What could be the problem.

I think you need to re-read that chapter of your book, or get a better book.

Ben Pope
 
R

Rolf Magnus

Kumar said:
Hi,

I am using a singleton class like the one given below:

class singleton
{
static singleton *object;
singleton();

public:
getInstance();
~singleton();
};


From some other function i try to do the following:

singleton *p= singleton :: getInstance();
detele p;


But here my program dumps. What could be the problem.

That depends on the implementation the getInstance() function.
 
D

Dave Townsend

Kumar Saurabh said:
Hi,

I am using a singleton class like the one given below:

class singleton
{
static singleton *object;
singleton();

public:
getInstance();
~singleton();
};


From some other function i try to do the following:

singleton *p= singleton :: getInstance();
detele p;


But here my program dumps. What could be the problem.

Thanks and Regards,
Saurabh

If you program craps out deleting p, then p is bad, either its not
initialized
or you've already deleted it or perhaps its pointing to something which is
not allocated ( like a static object ). Post the entire listing of your
code.
 
J

Jaspreet

Kumar said:
Hi,

I am using a singleton class like the one given below:

class singleton
{
static singleton *object;
singleton();

public:
getInstance();
~singleton();
};


From some other function i try to do the following:

singleton *p= singleton :: getInstance();
detele p;


But here my program dumps. What could be the problem.

Thanks and Regards,
Saurabh

Could we have the code please ? Did you intialise object ? Remember it
is static and you need to intialise/allocate it outside your class
declaration.
 
S

shankha

it would be nice if you can get us the code .. Have gdb running on your
executable it would tell you how it is happening ..(PRINT p's address
every time )
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,614
Members
45,292
Latest member
EttaCasill

Latest Threads

Top