Microsoft KB Archive/120964

{|
 * width="100%"|

BUG: Destructor Called Unexpectedly with Local Static Array

 * }

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

Issue type :

Technology : kbVCsearch kbAudDeveloper kbCVCComp