1

Possible Duplicate:
Does C++ call destructors for global and class static variables?

What is the lifetime of

  • global MyClass myclass;
  • global static MyClass myclass;
  • global const MyClass myclass;
  • global static const MyClass myclass;
  • function local static MyClass myclass; when its initialization actually occured
  • global static constexpr MyClass myclass; in C++11

and especially will they be destroyed on regular program end (i.e. main is left without an error)? Where does the standard states so.

I noticed that a private destructor prevents the creation of all those variables. But if I remember correctly it was explicitly mentioned somewhere that some static data may be put into a static data section and loaded pre-constructed, already. This would imply for me that no destructor would be called. And this would imply I am allowed to define such a variable...

Community
  • 1
  • 1
towi
  • 21,587
  • 28
  • 106
  • 187
  • You could put a `cout` statement in the destructor, and see for yourself. – Beta Oct 04 '12 at 13:40
  • 6
    @Beta "Try it" often fails in C and C++ due to implementation defined and undefined behavior. –  Oct 04 '12 at 13:41
  • 1
    @Beta you are right of course. Let me add in my question that I want to know where to look in the standard. – towi Oct 04 '12 at 13:41
  • @delnan: good point (although in this case it is well defined). – Beta Oct 04 '12 at 13:55

2 Answers2

6

The destructors for objects with static lifetime (all of the cases you mention define objects with static lifetime—although I don't think that an object in a constexpr can have a non-trivial destructor) are called from within exit(), in the reverse order the objects were constructed.

Returning from main causes exit to be called with the return value, so returning from main will cause these destructors to be called. Other means of program termination (abort(), assertion failure, _exit(), etc.) will not call destructors.

If the objects are in a DLL (.so under Unix), the destructors will normally be called when the DLL is unloaded.

James Kanze
  • 150,581
  • 18
  • 184
  • 329
  • Why on earth did I not think of *"an object in a `constexpr` can [not] have a non-trivial destructor"*. Not a good example, I have to think about that. – towi Oct 05 '12 at 07:10
  • @towi I'm not at all sure about it. I haven't looked it up, and I'm not sure where I'd have to start looking. But logically: to be used as a `constexpr` means that the value can be known at compile time, which in turm means that it's invariant. And a non-trivial destructor would mean that the value would cease to exist. – James Kanze Oct 05 '12 at 07:18
  • 1
    @Towi I've just checked. A `constexpr` variable must have a _literal type_ (§7.1.5/9), and one of the constraints for a type to be a literal type is that it have a trivial destructor (§3.9/10). – James Kanze Oct 05 '12 at 07:30
  • +1 for the detail, though it's relevant to note C++14 relaxed a lot of restrictions on what can be a `constexpr`. – underscore_d Feb 20 '16 at 18:00
4

The destructors of file or namespace scope objects get called when the control flow leaves main().

If an exception leaves main() then it's implementation defined whether the destructors of any objects get called. With modern compilers the destructors won't be called in this case to allow easy inspection of the program state when the unhandled exception was thrown. Early C++ implementations used exception mechanism based on setjmp/longjmp which would unwind the stack while searching for the exception handler and hence calling destructors even if no suitable exception handler was eventually found.

If an application terminates with _exit() or _Exit() or std::quick_exit() no destructors get called.

Maxim Egorushkin
  • 131,725
  • 17
  • 180
  • 271
  • 2
    The standard says that if an exception is not caught, `std::terminate()` is called. `std::terminate()`, by default, calls `abort()`. And `abort()` is not allowed to call destructors. – James Kanze Oct 04 '12 at 14:02
  • 1
    Also, of course: destructors get called when `exit()` is called. Returning from `main` causes `exit` to be called. – James Kanze Oct 04 '12 at 14:04
  • @JamesKanze: Right, thanks. The functions Maxim mentioned are probably in a specific implementation which then call the standard functions. – towi Oct 05 '12 at 07:12
  • @towi `_Exit()` and `std::quick_exit()` are standard C++; the former is also standard C. `_exit()` is standard Unix. – James Kanze Oct 05 '12 at 07:34
  • @JamesKanze: oh, you are right. found it 15.4 and 18.1. in c++11 std. – towi Oct 06 '12 at 18:11