singleton initialization

Z

Zeppe

Eric said:
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.

There seems not to be anything bad in your code (except from not having
the choice of deallocating the pointer, doing that way). Could it be
some static variable initialization order in the context in which you
use it?

Regards,

Zeppe
 
M

mlimber

[cross-posting deleted]

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()

}

If you run this code and only this code, it crashes sporadically? If
so, what compiler are you using and what options? If not, post a
complete but minimal sample that demonstrates the problem (see FAQ
5.4), since it may not be directly related to the singleton business
even if it appears that way.

Cheers! --M
 
E

Eric

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()
}
 
B

bruno.meneguello

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()

}

try:

class CTestClass
{
public:
static CTestClass* instance()
{

if (!m_instance) m_instance = new CTestClass;
return m_instance;
}
private:
static CTestClass* m_instance;
CTestClass() { /* do some stuff */ }

};

It must works.
 
S

Salt_Peter

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()

}

By 'race conditions', did you think that some local persistant
variable is immune to thread locks because its static?
 
F

Fei Liu

Eric said:
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;

This doesn't make sense. How can the compiler create this object at
compile time? Change this to:
static CTestClass& instance()
{
static CTestClass* m_instance;
if(!m_instance) m_instance = new CTestClass;
return m_instance;
}
 
A

Alf P. Steinbach

* Fei Liu:
This doesn't make sense. How can the compiler create this object at
compile time?

The object is created at run time.

Change this to:
static CTestClass& instance()
{
static CTestClass* m_instance;
if(!m_instance) m_instance = new CTestClass;
return m_instance;
}

This merely replicates the code generated by the compiler for the
original, but in a more verbose and possibly less efficient way.

See §6.7/4, "is initialized the first time control passes through its
declaration".

Hth.,

- Alf, 16.05.2007 00:22

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
 
J

John Moeller

Salt_Peter said:
By 'race conditions', did you think that some local persistant
variable is immune to thread locks because its static?

It's immune to race conditions because he hasn't started any other
threads yet (assuming that he doesn't try to start any threads from
static initializers).

--

John Moeller
(e-mail address removed)

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
 
J

John Moeller

Eric said:
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 */ }
};

I'm not sure if you're attempting to use a "Meyers Singleton," which is
very similar to what you are doing here, but that style singleton just
constructs the object locally, instead of allocating it on the heap:

....
static CTestClass & instance()
{
static CTestClass m_instance;
return m_instance;
}
....
int main()
{
CTestClass::instance(); // initialize singleton here to
avoid future race conditions
// sporadically crashes here when using CTestClass::instance()
}

It's a memory leak (there's no matching delete, not that it matters with
singletons most of the time, though it can), and constructing it locally
will solve that.

I can't claim to know why your program crashes, however. I tried
running your original program repeatedly on my platform, and no such
crash happened. It may be that your platform dislikes leaked memory.

--

John Moeller
(e-mail address removed)

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
 
K

Keith H Duggar

Alf said:
Fei Liu:

This merely replicates the code generated by the compiler for the
original, but in a more verbose and possibly less efficient way.

And it's still more verbose and possibly less efficient than:

static CTestClass& instance()
{
static CTestClass m_instance;
return m_instance;
}

which also destroys the instance properly and automatically.

KHD
 
J

James Kanze

And it's still more verbose and possibly less efficient than:
static CTestClass& instance()
{
static CTestClass m_instance;
return m_instance;
}
which also destroys the instance properly and automatically.

And thus introduces order of destruction issues. As a general
rule, it is preferable that the instance of a singleton NOT be
destructed. Ever.
 
B

Bob Hairgrove

{ Edits: double-spacing removed. -mod/aps }

And thus introduces order of destruction issues. As a general
rule, it is preferable that the instance of a singleton NOT be
destructed. Ever.

Won't the destructor of a static singleton object be called
automatically at program shutdown?
 
F

Fei Liu

{ Edits: quoted signature removed. -mod }
* Fei Liu:

The object is created at run time.



This merely replicates the code generated by the compiler for the
original, but in a more verbose and possibly less efficient way.

See §6.7/4, "is initialized the first time control passes through its
declaration".

Ah, so this is the C++ way of doing it...I may be wrong, but in C at
least for plain old datatype, function static variable initialization
happens at compile time.

Fei
 
F

Francis Glassborow

Fei said:
Ah, so this is the C++ way of doing it...I may be wrong, but in C at
least for plain old datatype, function static variable initialization
happens at compile time.
You sure about that? Sorry I am moving house and all my reference books
are packed away, but that would mean that a function static could only
be initialised with a const expression. I thought that functions statics
were initialised the first time the flow of execution passes through them.
 
F

\fr3

And thus introduces order of destruction issues. As a general
rule, it is preferable that the instance of a singleton NOT be
destructed. Ever.

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

James Kanze

Ah, so this is the C++ way of doing it...I may be wrong, but in C at
least for plain old datatype, function static variable initialization
happens at compile time.

Sort of. C doesn't actually say when the initialization occurs,
except to require that it occur before the variable first
becomes usable. Since it only allows constant expressions in
the initializers, there's no way a conforming program can find
out when the initialization actually occurs.

C++ has the same concept: if the type has a trivial constructor,
and it initialized with a constant expression, the
initialization is said to be static, and all that is required is
that it occur before the first time the variable can possibly be
used---in all of the implementations I know of, the initialized
object is laid out by the compiler are part of the binary image,
at compile time. If the constructor isn't trivial, however,
some code must be executed, and it suddenly because possible for
a program to know when it is executed. So C++ defined a precise
moment for this.
 
C

Carl Barron

Francis Glassborow said:
You sure about that? Sorry I am moving house and all my reference books
are packed away, but that would mean that a function static could only
be initialised with a const expression. I thought that functions statics
were initialised the first time the flow of execution passes through them.
The C99 standard does require the initializers of static variables to
be constant expressions [6.7.7 of c99]. So it is possible for a C
compiler to initailize all statics at compile time, removing a need to
test for first entry, but it is not requried by what I read unless
'may' means 'is'. [c99, section 6.7.8 para 4] reads:
/quote
All the expressions in an initializer for an object that has static
storage duration shall be constant expressions or string literals.
/endquote
 
M

mliptak

And thus introduces order of destruction issues. As a general
Won't the destructor of a static singleton object be called
automatically at program shutdown?

Unless it is allocated dynamically (e.g. by new).
 
P

Prasanna

Won't the destructor of a static singleton object be called
automatically at program shutdown?
I do not quite understand how you would run into issues when
destructing the instance of a singleton. Kindly elaborate.

Prasanna.
 

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

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,218
Latest member
JolieDenha

Latest Threads

Top