Microsoft KB Archive/155292: Difference between revisions

From BetaArchive Wiki
(importing KB archive)
 
m (Text replacement - "&" to "&")
 
(3 intermediate revisions by the same user not shown)
Line 73: Line 73:
The best way to work around the problem is to move the new statement to be within the body of the constructor. For example,
The best way to work around the problem is to move the new statement to be within the body of the constructor. For example,
<pre class="codesample">  ClassC() {
<pre class="codesample">  ClassC() {
       mClassB = new ClassB(&quot;Goodbye&quot;);
       mClassB = new ClassB("Goodbye");
   }
   }
                 </pre>
                 </pre>
Line 101: Line 101:
<pre class="codesample">  /* Compile options needed: /D_DEBUG
<pre class="codesample">  /* Compile options needed: /D_DEBUG
   */  
   */  
   #include &lt;afx.h&gt;
   #include <afx.h>
   #include &lt;iostream.h&gt;
   #include <iostream.h>


   #ifdef _DEBUG
   #ifdef _DEBUG
Line 110: Line 110:
   class ClassA {
   class ClassA {
   public:
   public:
       ClassA(const char *) { cout&lt;&lt; &quot;ClassA Constructor&quot; &lt;&lt; endl; }
       ClassA(const char *) { cout<< "ClassA Constructor" << endl; }
       ~ClassA()            { cout&lt;&lt; &quot;ClassA Destructor&quot; &lt;&lt; endl; }
       ~ClassA()            { cout<< "ClassA Destructor" << endl; }
   };
   };


   class ClassB {
   class ClassB {
   public:
   public:
       ClassB(const ClassA&amp;){ cout &lt;&lt; &quot;ClassB Constructor&quot; &lt;&lt; endl;}
       ClassB(const ClassA&){ cout << "ClassB Constructor" << endl;}
       ~ClassB()            { cout &lt;&lt; &quot;ClassB Destructor&quot; &lt;&lt; endl;}
       ~ClassB()            { cout << "ClassB Destructor" << endl;}
   };
   };


Line 124: Line 124:
       ClassB *mClassB;
       ClassB *mClassB;
       ClassC()
       ClassC()
             :mClassB(new ClassB(&quot;Goodbye&quot;) )    // Error
             :mClassB(new ClassB("Goodbye") )    // Error
       {
       {
       //    mClassB = new ClassB(&quot;Goodbye&quot;);  // Workaround
       //    mClassB = new ClassB("Goodbye");  // Workaround
       }
       }
       ~ClassC() { delete mClassB; }
       ~ClassC() { delete mClassB; }

Latest revision as of 12:28, 21 July 2020

Article ID: 155292

Article Last Modified on 12/10/2003



APPLIES TO

  • Microsoft Visual C++ 4.0 Standard Edition
  • Microsoft Visual C++ 4.1 Subscription
  • Microsoft Visual C++ 4.2 Enterprise Edition
  • Microsoft Visual C++ 5.0 Enterprise Edition
  • Microsoft Visual C++ 4.2 Professional Edition
  • Microsoft Visual C++ 5.0 Professional Edition



This article was previously published under Q155292

SYMPTOMS

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

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

RESOLUTION

The 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");
   }
                

STATUS

Microsoft 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 and Visual C++ 6.0.

For more information, please see the following article in the Microsoft Knowledge Base:

170365 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: kbbug kbfix kbvs97sp3fix kbvc600fix kbcodegen kbcpponly kbcode kbcompiler KB155292