Singleton question.

P

prawit.c

Hi, all
I've written a program to produce singleton object.
Like this.

---------------------------------------------------
class A{
public:
static A* getInstance();
void print(); \\ print out 'a' I won't implement here.
void setA(int); \\ set 'a', I won't implement here.
~A(){}
protected:
A(){}
private:
int a;
static A* instance;
};

A* A::instance = NULL;

A* A::getInstance()
{
if(instance)
return instance;
else
return (instance=new A());
}

int main()
{
A * a = A::getInstance();
a->setA(10);
a->print();
delete a; // <-- This line is a question. Read below.

a->setA(15); // <-- This line should crash, isn't it?
a->print(); // <-- This line should crash, isn't it?

return 0;
}
-----------------------------------------------------------
The result of this program is:
#./SingleTon
10
15
#
------------------------------------------------------------
My question is:
"Why instance of class A still exist? "
My understanding is we've deleted that object (line 'delete a;')


Thank in advance.
Prawit Chaivong.
 
S

Srini

delete a; // <-- This line is a question. Read below.
a->setA(15); // <-- This line should crash, isn't it?
a->print(); // <-- This line should crash, isn't it?

return 0;
}
-----------------------------------------------------------
The result of this program is:
#./SingleTon
10
15
#

This is undefined behavior. One cannot expect anything definitive when
you access members after deleting an object. Its a different matter
that you _should not_ do that!

Regards,
Srini
 
P

prawit.c

What do you think if I prevent this object from deleting.
By declare Dtor as protected.
 
S

Srini

What do you think if I prevent this object from deleting.

You can return a static reference instead of a pointer to the lone
object.

static A& A::getInstance(void)
{
static A instance;
return instance;
}

But this method will have implications in a multi-threaded application.

By declare Dtor as protected.

Destructors must NOT be made private! The compiler would not allow you
to do that.

Regards,
Srini
 
O

Old Wolf

Srini said:
Destructors must NOT be made private! The compiler would not allow you
to do that.

Destructors can be protected or private, and the compiler
would allow it. It would be useful in exactly this case,
to prevent the main program from deleting the object.
 
S

Srini

Destructors can be protected or private, and the compiler
would allow it. It would be useful in exactly this case,
to prevent the main program from deleting the object.

I stand corrected. Only if there's an object instance that needs to be
destroyed and the compiler finds the destructor is private, does it
throw up an error. Thanks Old Wolf!

Srini
 
U

Usenet

Srini said:
You can return a static reference instead of a pointer to the lone
object.

static A& A::getInstance(void)
{
static A instance;
return instance;
}

But this method will have implications in a multi-threaded application.

Can you tell about some implications plz?
 
S

Srini

But this method will have implications in a multi-threaded application.
Can you tell about some implications plz?

The version of the singleton implementation that returns a static
pointer, would use operator new to allocate the lone object. Because
operator new is thread-safe it can be safely used in a multithreaded
app. Moreover with the static pointer version, its our responsibility
to delete the lone object. But with the second version, it gets
destructed automatically when the app terminates.
 
P

prawit.c

I don't think a caller has responsibility to delete that object since
it might be used in somewhere else.
 
S

Srini

I don't think a caller has responsibility to delete that object since
it might be used in somewhere else.

I did not mean the 'caller'. Whoever writes such a class must provide
some means for cleaning up.

Srini
 
L

Laurens

You could assign the instance to a global or module-level auto_ptr (or
boost::scoped_ptr) to have it cleaned up automatically upon program
termination.

Regards
-Laurens
 

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,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top