BUG: Destructor Called Unexpectedly with Local Static Array

ID: Q120964

7.00 | 1.00 1.50 MS-DOS | WINDOWS kbtool kbbuglist kbcode

The information in this article applies to:

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:

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

Last Reviewed: July 23, 1997