KnowledgeBase Archive

An Archive of Early Microsoft KnowledgeBase Articles

View on GitHub

Q236114: FIX: Destructor of Static Object Not Called on Exit

Article: Q236114
Product(s): Microsoft C Compiler
Version(s): 5.0,6.0
Operating System(s): 
Keyword(s): kbCompiler kbCPPonly kbVC500bug kbVC600bug kbDSupport kbGrpDSVCCompiler kbNoUpdate
Last Modified: 11-FEB-2002

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

- Microsoft Visual C++, 32-bit Enterprise Edition, versions 5.0, 6.0 
- Microsoft Visual C++, 32-bit Professional Edition, versions 5.0, 6.0 
- Microsoft Visual C++, 32-bit Learning Edition, version 6.0 
-------------------------------------------------------------------------------

SYMPTOMS
========

The destructor for static local variables in static objects may never be called.
The destructor should be called when the program exits, but it does not get
called. See the More Information section below for a specific example.

RESOLUTION
==========

Avoid using this construct.

STATUS
======

Microsoft has confirmed this to be a bug in the Microsoft products listed at the
beginning of this article.

This problem was corrected in Microsoft Visual C++ .NET.

MORE INFORMATION
================

The static local won't be destroyed if it is local to a destructor for which the
only instance(s) of the destructor's class is static. If there are any local or
heap allocated instances, the object will be destroyed (assuming delete is
called for heap allocated instances).

The following code demonstrates the problem:

  #include <iostream>

  struct X {
  	X() {std::cout << "constructor X" << std::endl;} 
  	~X() {std::cout << "destructor X" << std::endl;} // destructor of X is never called.
  };

  struct Y {
     ~Y() {static X x;}
  };

  void main() {
     static Y y;
  }

Additional query words:

======================================================================
Keywords          : kbCompiler kbCPPonly kbVC500bug kbVC600bug kbDSupport kbGrpDSVCCompiler kbNoUpdate 
Technology        : kbVCsearch kbAudDeveloper kbVC500 kbVC600 kbVC32bitSearch kbVC500Search
Version           : :5.0,6.0
Issue type        : kbbug
Solution Type     : kbfix

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

THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Copyright Microsoft Corporation 1986-2002.