Destruction order between globals and static function locals

Rodrigo Salazar

My understanding is that the order of destruction of objects with static storage duration is the inverse of the order of their initialization.

In this code snippet:

#include <iostream>

class LogValuesObj {
public:
    LogValuesObj() {
        std::cout << "LogValuesObj" << std::endl;
    }
    ~LogValuesObj() {
        std::cout << "~LogValuesObj" << std::endl;
    }
};

void logValues() {
    static LogValuesObj o;
    std::cout << "logValues function called" << std::endl;
}

class EarlyInitLogValues {
public:
    EarlyInitLogValues() {
        std::cout << "EarlyInitLogValues" << std::endl;
        logValues();
    }
    ~EarlyInitLogValues() {
        std::cout << "~EarlyInitLogValues" << std::endl;
    }
};

EarlyInitLogValues e;


int main() {
    return 0;
}

The output on my compiler is:

EarlyInitLogValues
LogValuesObj
logValues function called
~EarlyInitLogValues
~LogValuesObj

In this case, the destruction order is the same as the init order, I expected it to be reversed. If both the global 'e' and the function static local 'o' have static storage duration, why is the destruction order not reversed?

Brian Bi

It is well-known that objects with static storage duration are destroyed in the reverse order of their construction. But to be more specific—and since it matters in your case—they are destroyed in the reverse order of the completion of their initialization. See [basic.start.term]/3.

In your case, the initialization of o will complete before the initialization of e. Therefore, e is destroyed before o.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

locals() vs globals() in function

What's the difference between globals(), locals(), and vars()?

difference between locals() and globals() and dir() in python

Globals and Locals

Why is the order of destruction of these function-local static objects NOT the inverse of their order of initialization?

What is the order of destruction of function arguments?

What is the order of destruction of function parameters?

Call to a [[noreturn]] function and order of destruction

globals(), locals(), vars() do not work as expected in a function of python

scope of globals, locals, use with exec()

Ensure the construction and destruction order of static variables in c++

Destruction of static members inside common classes between two shared objects?

eval globals and locals argument do not work as expected

about pandasql locals() and globals() method issue

locals() and globals() in stack trace on exception (Python)

How to execute a callable in a given context (locals and globals)?

Python3 globals() and locals() contents

python quits unexpectedly when using locals() or globals()

Destruction of local static objects

Order of static initialization with function call

Is it possible to invert order of destruction?

Order of destruction parent and child

Order of destruction of QML objects

Enforce destruction order

Destruction Order of Meyers Singletons

Globals between files in python

Static function variable initialization order in the same function

Is it possible to get locals() and globals() from an upper stack frame?

Using a list comprehension to look up variables works with globals() but not locals(). Why?