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…
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 onsetjmp/longjmpwhich 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()orstd::quick_exit()no destructors get called.