Search code examples
c++staticdestructormember

Destruction of static member in a class with global instance


I have a class with a static non-primitive member. For example:

class SomeObject
{
    ... // something that will be destroyed in destructor,
        // like an array pointer.

public:
    SomeObject();
    ~SomeObject(); 
};

class MyClass
{
    static SomeObject m_object;

public:
    MyClass();
    ~MyClass(); // this will access m_object

    static bool SetupStaticMember();
};

/// Implementation of SomeObject and MyClass ///

SomeObject MyClass::m_object;
bool dummy = MyClass::SetupStaticMember(); // Setup the values in m_object, for example,
                                           // allocate memory that will be released in ~SomeObject().

MyClass g_my_global_class;

g_my_global_class is declared as global variable, so it's destructor is called after leaving main(). However, MyClass::m_object is static, so it will also be destroyed after main().

Is there any guarantee that ~MyClass() will execute before ~SomeObject() from MyClass::m_object? In other words, when the destructor of a global class instance is called, can I assume that the static members of this class are still there to be accessed, or does this depend on construction/destruction orders?

If the codes are written in this order, I think that g_my_global_class is constructed later, so it should be destructed first. Do things change if the line

MyClass g_my_global_class;

moves to another .cpp file and its file name causes the order to change?


Solution

  • First,

    bool dummy = MyClass::InitStaticMember(); // m_object is initialized here
    

    Doesn't actually intialize the static member. That happens at the line before

    SomeObject MyClass::m_object;
    

    So since you essentially have

    SomeObject MyClass::m_object;
    MyClass g_my_global_class;
    

    and since objects are destroyed in the reverse order then g_my_global_class is destroyed before MyClass::m_object.

    Now, if you move MyClass g_my_global_class; to a different translation unit then all bets are off. The ordering is only guaranteed in a single translation unit.