FIX: Destructor Not Called When DEBUG_NEW Is DefinedLast reviewed: December 1, 1997Article ID: Q155292 |
The information in this article applies to:
SYMPTOMSIf new is defined as DEBUG_NEW and a call to new is made for a class that takes a reference to another class in the member initialization, then the destructor is not called for the referenced class for versions 4.0, 4.1, and 4.2. This is illustrated in the output from the following example:
ClassA Constructor ClassB Constructor ClassB DestructorThe destructor for ClassA is not called. NOTE: The Visual C++ 5.0 compiler generates the following error if you try to compile the sample code:
fatal error C1001: INTERNAL COMPILER ERROR (compiler file 'E:\utc\src\\P2\ehexcept.c', line 516) RESOLUTIONThe best way to work around the problem is to move the new statement to be within the body of the constructor. For example,
ClassC() { mClassB = new ClassB("Goodbye"); } STATUSMicrosoft has confirmed this to be a bug in the Microsoft products listed at the beginning of this article. This bug has been fixed in Visual Studio 97 Service Pack 3. For more information, please see the following article in the Microsoft Knowledge Base:
ARTICLE-ID: Q170365 TITLE : INFO: Visual Studio 97 Service Packs - What, Where, and Why MORE INFORMATION
Sample Code
/* Compile options needed: /D_DEBUG */ #include <afx.h> #include <iostream.h> #ifdef _DEBUG #define new DEBUG_NEW #endif class ClassA { public: ClassA(const char *) { cout<< "ClassA Constructor" << endl; } ~ClassA() { cout<< "ClassA Destructor" << endl; } }; class ClassB { public: ClassB(const ClassA&){ cout << "ClassB Constructor" << endl;} ~ClassB() { cout << "ClassB Destructor" << endl;} }; class ClassC { public: ClassB *mClassB; ClassC() :mClassB(new ClassB("Goodbye") ) // Error { // mClassB = new ClassB("Goodbye"); // Workaround } ~ClassC() { delete mClassB; } }; void main() { ClassC TestClassInstance; } Keywords : CodeGen CPPIss vcbuglist400 vcbuglist500 VS97FixlistSP3 Version : 4.0 4.1 4.2 5.0 Platform : NT WINDOWS Issue type : kbbug Solution Type : kbfix kbservicepack |
================================================================================
© 1998 Microsoft Corporation. All rights reserved. Terms of Use. |