Microsoft KB Archive/236114

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

Article ID: 236114

Article Last Modified on 12/11/2003

-

APPLIES TO


 * Microsoft Visual C++ 5.0 Enterprise Edition
 * Microsoft Visual C++ 6.0 Enterprise Edition
 * Microsoft Visual C++ 5.0 Professional Edition
 * Microsoft Visual C++ 6.0 Professional Edition
 * Microsoft Visual C++ 6.0 Standard Edition

-



This article was previously published under Q236114



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 that this is a bug in the Microsoft products that are 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:


 * 1) include

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; }

Keywords: kbbug kbfix kbnoupdate kbcpponly kbcompiler KB236114

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.