singleton initialization

J

James Kanze

Would you elaberate on your preference further? Which I failed
to see as a good generic solution.

What is there to elaborate on? If the singleton object is
destructed, you potentially have order of destructor problems.
So it's usually a good idea to create them in a way that they
won't get destructed.
 
S

Seungbeom Kim

Yes. But the problem here is the order of destruction of static object.
I do not quite understand how you would run into issues when
destructing the instance of a singleton. Kindly elaborate.

Static objects are destroyed in the reverse order of construction.
While this may seem perfectly reasonable, sometimes it is not; suppose
for example you have a Server singleton and a Client singleton, where
the latter uses the former. Consider the following scenario:

Client::instance().use_server()
Client::Client()
// now Client constructed
Client::use_server()
Server::instance().serve_client()
Server::Server()
// now Server constructed
Server::serve_client()

Here, Server is constructed after Client, so at program shutdown, Server
is destroyed before Client. Now suppose the destructor of Client tries
to use Server, which doesn't exist any more.

Server::~Server()
// now Server destroyed
Client::~Client()
Server::instance().serve_client() // boom!

The right thing to happen is that Server should outlive Client, whether
Server is created before Client or after Client. One way to ensure that
is to let the Client's constructor use Server at least once, which
ensures that Server is constructed before Client. (And an object is
considered to be constructed when its constructor finishes, without
throwing an exception.)
 
M

myork

{ Edits: quoted clc++m banner removed. Please quote only relevant text.
-mod }

I created a singleton class as in the example below. The application
sporadically crashes in the second line of the main function as shown.
However, when I change the singleton such that the static pointer is a
class member (defined in the cpp file) and the instance function
creates the object if the pointer is NULL, then it works fine. I would
appreciate any explanations as to why this happens.

class CTestClass
{
public:
static CTestClass& instance()
{
static CTestClass* m_instance = new CTestClass;
return *m_instance;
}
private:
CTestClass() { /* do some stuff */ }

};

int main()
{
CTestClass::instance(); // initialize singleton here to
avoid future race conditions
// sporadically crashes here when using CTestClass::instance()

}

There is not enough real information to find out why it sporadically
crashes.
Sporadically suggests: There is a timing issue which suggests threads
are involved, and since I don't see any in main it suggests they are
created by constructors of global variables (But again not enough
information).
Moving the pointer to class static: As this seems to solve the problem
this suggests there is a memory corruption somewhere else. This
becomes hard to track down without other tools.

What does jump out. Is that the "Copy Constructor" and "Assignment
Operator" are not defined in your singelton. Thus some global object
may by making a copy of the singelton (in its constructor) and the
destruction of this copy may be interfering with the singelton
(especially if your singelton contains raw pointers).
 
S

Sarath

{ Edits: quoted clc++m banner removed. Please don't quote the banner.
Also, please don't top-post in this group, see FAQ item 5.4. -mod }

Hello,
Make sure that your destructor also made private to avoid 'deleting
the instance pointer.

Regards,
Sarath
 
N

neelsmail

CTestClass() { /* do some stuff */ }

By any chance the "/* do some stuff */" is the reason why it crashes?
Did you try to test it without doing _any_ stuff in constructor?

Thanks,
Neel.
 

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

Similar Threads


Members online

No members online now.

Forum statistics

Threads
474,294
Messages
2,571,511
Members
48,206
Latest member
EpifaniaMc

Latest Threads

Top