C++ Object Management – Early Destruction of Objects in C++

cglobals

I am not sure if this is the right forum of this question, but I will try here since this question about the c++ language. The problem:

I use one global variable in my code which is a graphics object. The thing is that when I shut down the program I tried to delete all the created objects (in a function called close, since I wanted to finish some processes from an C API as well). However, when I deleted that global graphics object I got a memory access violation error at the end of main. The reason for this was that the destructor was called again when main ended. This was something that I was not aware of. This led me to the for this program completely irrelevant (since I still wanted to keep the variable until the GUI closed), but for c++ programming really relevant question; Is it impossible to destroy a global c++ object before the end of main?

However, I read this

Static: Objects declared in global or namespace scope (§6.3.4) and statics declared in func- tions (§12.1.8) or classes (§16.2.12) are created and initialized once (only) and ‘‘live’’ until the program terminates (§15.4.3). Such objects are called static objects. A static object has the same address throughout the life of a program execution. Static objects can cause serious problems in a multi-threaded program because they are shared among all threads and typically require locking to avoid data races (§5.3.1, §42.3)

However, though versions of this text appear all over on the internet my question is not completely answered by this explanation. Though it may be implied, since global variables always seem to be static it would be nice to have a straight out answer.

Best Answer

However, though versions of this text appear all over on the internet my question is not completely answered by this explanation. Though it may be implied, since global variables always seem to be static it would be nice to have a straight out answer.

If you didn't explicitly create it, you don't need to explicitly destroy it. Let's just remove some of the words from your standard quote:

Static: Objects declared in global or namespace scope (§6.3.4) ... are created and initialized once (only) and ‘‘live’’ until the program terminates (§15.4.3). Such objects are called static objects.

So, if you declare an object with global or namespace scope, it is static, and its lifetime is roughly the program lifetime.


By analogy, consider normally-scoped local variables:

void foo() {
    std::string s;
    // code
}

now there is no way to destroy s without exiting its enclosing scope. If you explicitly destroy it in-place, the destructor will still be called again when the scope exits.

Now, consider global and namespace scope to be the top-level scope of your program. You enter this scope before main (which is nested inside the global scope) and exit it after leaving main. This top-level scope lives exactly as long as the program, and you're no more able to destroy the its objects early than you are objects in any other scope.


There is some subtlety about exactly when the constructor gets called (generally before main starts, with a fudge to allow dynamically-loaded libraries without having to talk about platform specifics), when the destructor gets called (relative to exit handlers, say), and the relative ordering of these for different statics (not well-defined, so don't make global initialization or destruction depend on other globals).


Of course, if you want the object to be global for convenience, you can still choose to have explicit setup/cleanup calls instead of using the ctor/dtor for this.

Related Topic