BUG: Destructor Called Unexpectedly with Local Static Array

Last reviewed: July 22, 1997
Article ID: Q120964
7.00 | 1.00 1.50 MS-DOS | WINDOWS kbtool kbbuglist kbcode

The information in this article applies to:

  • The Microsoft C/C++ Compiler (CL.EXE), included with:

        - Microsoft C/C++ for MS-DOS, version 7.0
        - Microsoft Visual C++ for Windows, versions 1.0 and 1.5
    

SYMPTOMS

Assume that there exists a class D that provides a constructor, a destructor, and a show() function to display its data. If a static array of class D is defined within the body of a function with an initializer, then the destructor for the class is called on an unknown object. The destructor is called after the construction of the first object, but before the construction of any remaining objects. The sample code in the More Information section below demonstrates this problem.

RESOLUTION

Any one of the following workarounds prevents this from occurring in the sample code provided in the More Information section below:

  • Remove the "static" keyword from the definition of the array.

          -or-
    
  • Move the definition of the array so that it has file scope instead of local scope; that is, change it from a local variable to a global variable.

          -or-
    
  • Remove the intializer from the array definition and initialize the elements separately.

STATUS

Microsoft has confirmed this to be a bug in the Microsoft products listed at the beginning of this article. We are researching this bug and will post new information here in the Microsoft Knowledge Base as it becomes available.

This is not a problem in the 32-bit compilers.

MORE INFORMATION

The following sample code demonstrates the problem.

NOTE: The problem doesn't occur when the objects are allocated dynamically.

Sample Code

/* Compile options needed: none
*/

#include <iostream.h>

class D {
   double data;  // This is private

 public:
   D(double u) : data(u)
   {
      cout << "In Constructor, data=" << data << endl;
   }

   ~D()
   {
      cout << "In Destructor, data=" << data << endl;
   }

   void show() const
   {
      cout << "\tdata=" << data << endl;
   }
};

void main()
{
   static D t[2] = { 0.0, 1.0 };
   t[0].show();
   t[1].show();
}


Additional reference words: 1.00 1.50 7.00
KBCategory: kbtool kbbuglist kbcode
KBSubcategory: CPPIss
Keywords : kb16bitonly


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.

Last reviewed: July 22, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.