There is a great FAQ(isocpp) just demonstrates why we potentaily cannot we use static object instead of static pointer to avoid Fiasco problem. I came across a sentence in the answer that gave me a headache
.... By changing the declaration from
static X* xp = new X();
tostatic X xp;
, we still correctly handle the initialization situation but we no longer handle the deinitialization situation. For example, if there are 3 static objects, say a, b and c, that use ans during their destructors, the only way to avoid a static deinitialization disaster is ifxp
is destructed after all three.
main.cpp
#include "x.h" // struct X { X(int); void f(); };
X& x(int i)
{
static X xp{ i };
return xp;
}
struct Y
{
Y(int);
private: int m_y;
};
Y::Y(int i) {
x(i).f();
}
other.cpp
Y y{ 20 };
struct X {
X(int);
void f();
private: int m_x;
};
X::X(int j): m_x(j) {};
void X::f() { std::cout << m_x << std::endl; }
Then they continue with:
The point is simple: if there are any other static objects whose destructors might use
xp
afterxp
is destructed, bang, you’re dead.
I just cannot understand how this problem can happen. I need a practical example that demonstrates how this problem can occur.