63

From my example program, it looks like it does call the destructors in both the cases. At what point does it call the destructors for global and class-static variables since they should be allocated in the data section of the program stack?

Khaled Alshaya
  • 94,250
  • 39
  • 176
  • 234
user236215
  • 7,278
  • 23
  • 59
  • 87

2 Answers2

73

From § 3.6.3 of the C++03 standard:

Destructors (12.4) for initialized objects of static storage duration (declared at block scope or at namespace scope) are called as a result of returning from main and as a result of calling exit (18.3). These objects are destroyed in the reverse order of the completion of their constructor or of the completion of their dynamic initialization. If an object is initialized statically, the object is destroyed in the same order as if the object was dynamically initialized. For an object of array or class type, all subobjects of that object are destroyed before any local object with static storage duration initialized during the construction of the sub- objects is destroyed.

Furthermore, § 9.4.2 7 states:

Static data members are initialized and destroyed exactly like non-local objects (3.6.2, 3.6.3).

However, if a destructor has no observable behavior, it may not be invoked. Terry Mahaffey details this in his answer to "Is a C++ destructor guaranteed not to be called until the end of the block?" .

Community
  • 1
  • 1
outis
  • 75,655
  • 22
  • 151
  • 221
  • 2
    Just as an observation: Visual C++ 2010 seems to ignore this standard (among others). I couldn't catch the breakpoint in a test destructor. See: http://pastebin.com/sCMFYhzZ – progician Dec 07 '11 at 11:46
  • 1
    @progician: did the destructor have observable behavior? It's possible that VC++ '10 was allowed to elide the destructor call. Try printing a prompt then reading input, or opening a file & writing to it in the destructor. – outis Dec 07 '11 at 12:29
  • 1
    ... Of course, I wouldn't be too surprised if VC++ ignored the standard. – outis Dec 07 '11 at 12:46
  • FWIW I was having exactly this problem with VS2008SP1. Added a printf, still no function. This is issueful, as I'm trying to call curl_global_cleanup() as per their documentation. – Mark McKenna Apr 13 '12 at 19:36
5

Somewhere after "main"

(you can't know or rely on the exact order in which they are called)

Vi.
  • 37,014
  • 18
  • 93
  • 148
  • 8
    It happens _before_ `std::cout` is destroyed. I.e. you can print stuff there. – MSalters Feb 05 '10 at 11:50
  • 1
    @MSalters I assume that's just a detail of your particular implementation and/or the order in which you declared your objects relative to `cout` or the first call that invokes its constructor ( e.g. `operator<<()` ). I doubt it's guaranteed, in which case, I'd reemphasise the "can't rely on" in this answer. – underscore_d Feb 20 '16 at 17:55
  • 2
    @underscore_d: No, it's definitely a guarantee. It's possible because the Standard Library is special / known to the implementation. – MSalters Feb 21 '16 at 16:44
  • 1
    @MSalters Thanks, your comment pointed me in the direction of this: http://stackoverflow.com/questions/8784892/is-stdcout-guaranteed-to-be-initialized The top answer and Jerry Coffin's comment on the 2nd answer seem to be the most relevant bits. That's my new thing learned for today! – underscore_d Feb 21 '16 at 18:56