INF: exit() Function Does Not Clean Up Non-Static Objects
PSS ID Number: Q102667
Article last modified on 07-14-1994

7.00 8.00 8.00c | 8.00

MS-DOS          | WINDOWS NT


----------------------------------------------------------------------
The information in this article applies to:

 - Microsoft C/C++ for MS-DOS, version 7.0
 - Microsoft C/C++ Compiler for MS-DOS, versions 8.0 and 8.0c (included
   with Visual C++ for Windows, versions 1.0 and 1.5)
 - Microsoft C/C++ Compiler for Windows NT, version 8.0 (included with
   Visual C++ for Windows NT, version 1.0)
----------------------------------------------------------------------

When an application calls the exit(), _exit(), _c_exit() or _cexit()
function, the application does not call the destructors for any
temporary or automatic objects that exist at the time of the call. The
code sample below demonstrates this behavior.

An "automatic" object is an object that is declared in a function when
the "static" keyword is not present. A temporary object is an object
the compiler creates.

To properly destroy an automatic object before calling exit(),
explicitly call the destructor for that object. For example:

   myObject.myClass::~myClass();

Sample Code
-----------

/*
 * Compiler options needed: /f /Od /Zi
 */

#include <iostream.h>
#include <process.h>

class myClass
{
   int nVal;

public:
   myClass();
   void View() {cout << nVal << "\n";};
   ~myClass();
};

myClass::myClass()
{
   nVal = 99;
}

myClass::~myClass()
{
   cout << "Destructor has been called\n";
}

void main(void)
{
   myClass myObject;

   myObject.View();

// Remove the comment from the next line to call the destructor
// myObject.myClass::~myClass();

   exit(0);     // Comment out this line to call the destructor
}

Additional reference words: 1.00 1.50 7.00 8.00 8.00c
KBCategory: Tls
KBSubcategory: CPPIss

=============================================================================

Copyright Microsoft Corporation 1994.
