TITLE: Destructor invocation upon exit PROBLEM: rad2r@uvacs.cs.Virginia.EDU (Robert DeLine) Sorry for this novice question, but when I call exit() from a program compiled using g++ 2.1, the destructors for my objects don't get called. This doesn't surprise me too much, but is this correct? Does the ARM mention what should happen when exit() is called? What if the program is ended due to an uncaught exception? If destructors _are_ called when leaving via an uncaught exception, perhaps this is a better way than calling exit() to leave from deep inside a program? RESPONSE: rmartin@thor.Rational.COM (Bob Martin), 4 Sept 92 The destructors for static objects should be invoked when you call exit. However the destructors for objects which are declared as 'auto' variables (on the stack) in the function which calls exit, probably will not get invoked. For example. main() { MyClass A; static MyClass B; exit(); } The destructor for B should be invoked. But the destructor for A probably will not. I say probably, because a clever compiler might be able to figure out that, upon exit, the enclosing stack frames should be destroyed. The definition of the exception feature says that all destructors for objects created as auto variables between the throw and the catch will be invoked. Thus, "exit" does not behave the way an exception would. Presumably, an uncaught exception will be "caught" by the code that calls main. Thus, destructors will most likely be invoked for uncaught exceptions. I agree, when exceptions are implemented, the present a better mechanisms for exitting a program than embedded calls to 'exit' do. And remember, never use gotos. RESPONSE: steve@taumet.com (Steve Clamage) Auto objects are NOT destroyed when exit() is called, only static objects which have been constructed. My copy of the ARM is not handy, and I don't remember whether the bit about auto objects is clearly stated there. It is made explicit in the C++ Committee Working Paper, and it is very safe to assume the final Standard will say that.