TITLE: termination in the DWP (Draft C++ Working Paper) (Newsgroups: comp.std.c++) PROBLEM: bill@amber.ssd.hcsc.com (Bill Leonard) >I have a question about the order of destruction of local static variables. >Can a program rely on the order of destruction of local statics that are >declared in different functions? RESPONSE: clamage@Eng.Sun.COM (Steve Clamage), 16 June 96 The current draft says they are destroyed in reverse order of their construction. It doesn't matter whether they are in different translation units. LEONARD: >A related question is the relationship between the destruction of "x" and >"y" and the execution of any atexit functions. The April WP says: ... CLAMAGE: Here is what the current draft says, and I think it answers your questions: 3.6.3 Termination [basic.start.term] 1: Destructors (12.4) for initialized objects of static storage duration (declared at block scope or at namespace scope) are called when returning from main and when calling exit (18.3). These objects are destroyed in the reverse order of the completion of their constructors. 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 subobjects is destroyed. 2: If a function contains a local object of static storage duration that has been destroyed and the function is called during the destruction of an object with static storage duration, the program has undefined behavior if the flow of control passes through the definition of the previously destroyed local object. 3: If a function is registered with atexit (see , 18.3) then following the call to exit, any objects with static storage duration initialized prior to the registration of that function will not be destroyed until the registered function is called from the termination process and has completed. For an object with static storage duration constructed after a function is registered with atexit, then following the call to exit, the registered function is not called until the execution of the object's destructor has completed. 4: Where a C + + implementation coexists with a C implementation, any actions specified by the C implementation to take place after the atexit functions have been called take place after all destructors have been called. 5: Calling the function void abort(); declared in terminates the program without executing destructors for objects of automatic or static storage duration and without calling the functions passed to atexit())