M
MathWizard
Hi all,
This question relates to an earlier thread I started (static object as
member of a class).
I have the following (illustrative) code. Again, I hope to make no typos:
class A {
... //whatever here
};
class B {
... // omitted ctors, dtor, ....
A& get_static_A();
void cleanup();
};
A& B::get_static_A()
{
static A* A_ptr;
if (A_ptr == NULL) {
A_ptr = new A;
atexit(cleanup);
}
return *A_ptr;
}
This code is a result of the advice I got in my revious thread. Class B
needs to share one single copy of class A, and this is the way it should
all work correctly.
Problem: my compiler does not like the atexit call, it says: "cannot
find a match for atexit(void)" I also tried things like
"atexit(B::cleanup);", but that doesn't help. Any ideas?
Furthermore, I stumbled on an article at
http://www.petebecker.com/js/js199904.html
I cite: "Be very, very careful if you mix |atexit| functions and static
objects with destructors. The rules are pretty clear: functions
registered with |atexit| are interleaved with destructors for static
objects in the reverse order of their registration. If you create a
static object, then register an |atexit| function, then create another
static object, the compiler should generate code that invokes the
destructor for the second object, calls the |atexit| function, then
invokes the destructor for the first object. If your code relies on this
sequence being handled correctly it is very likely to crash. If you’re
concerned about writing portable code (as we are in The Journeyman’s
Shop) don’t rely on intermixing |atexit| functions with static destructors."
If I look at the code I wrote above, then I also rely on a strict
sequence: the cleanup method must be called before the static A is
destroyed. Comments on this?
Thanks for any pointers,
Jeroen
This question relates to an earlier thread I started (static object as
member of a class).
I have the following (illustrative) code. Again, I hope to make no typos:
class A {
... //whatever here
};
class B {
... // omitted ctors, dtor, ....
A& get_static_A();
void cleanup();
};
A& B::get_static_A()
{
static A* A_ptr;
if (A_ptr == NULL) {
A_ptr = new A;
atexit(cleanup);
}
return *A_ptr;
}
This code is a result of the advice I got in my revious thread. Class B
needs to share one single copy of class A, and this is the way it should
all work correctly.
Problem: my compiler does not like the atexit call, it says: "cannot
find a match for atexit(void)" I also tried things like
"atexit(B::cleanup);", but that doesn't help. Any ideas?
Furthermore, I stumbled on an article at
http://www.petebecker.com/js/js199904.html
I cite: "Be very, very careful if you mix |atexit| functions and static
objects with destructors. The rules are pretty clear: functions
registered with |atexit| are interleaved with destructors for static
objects in the reverse order of their registration. If you create a
static object, then register an |atexit| function, then create another
static object, the compiler should generate code that invokes the
destructor for the second object, calls the |atexit| function, then
invokes the destructor for the first object. If your code relies on this
sequence being handled correctly it is very likely to crash. If you’re
concerned about writing portable code (as we are in The Journeyman’s
Shop) don’t rely on intermixing |atexit| functions with static destructors."
If I look at the code I wrote above, then I also rely on a strict
sequence: the cleanup method must be called before the static A is
destroyed. Comments on this?
Thanks for any pointers,
Jeroen