グローバルオブジェクトのような関数静的オブジェクトは、破棄されることが保証されています (作成されていると仮定します)。
破壊の順序は創造の逆です。
したがって、オブジェクトが破棄中に別のオブジェクトに依存している場合、それがまだ使用可能であることを保証する必要があります。作成の順序が正しく行われていることを確認することで、破壊の順序を強制できるため、これは比較的簡単です。
次のリンクはシングルトンに関するものですが、同様の状況とその解決策について説明しています:
Finding C++ static initialization order problems
FAQ lite で説明されているように、遅延初期化されたグローバルの一般的なケースに外挿すると、次のような問題を解決できます。
namespace B
{
class B { ... };
B& getInstance_Bglob;
{
static B instance_Bglob;
return instance_Bglob;;
}
B::~B()
{
A::getInstance_abc().doSomthing();
// The object abc is accessed from the destructor.
// Potential problem.
// You must guarantee that abc is destroyed after this object.
// To gurantee this you must make sure it is constructed first.
// To do this just access the object from the constructor.
}
B::B()
{
A::getInstance_abc();
// abc is now fully constructed.
// This means it was constructed before this object.
// This means it will be destroyed after this object.
// This means it is safe to use from the destructor.
}
}
namespace A
{
class A { ... };
A& getInstance_abc()
{
static A instance_abc;
return instance_abc;
}
}