Microsoft KB Archive/168385

From BetaArchive Wiki
Knowledge Base


Article ID: 168385

Article Last Modified on 9/23/2003



APPLIES TO

  • Microsoft Visual C++ 5.0 Enterprise Edition
  • Microsoft Visual C++ 6.0 Enterprise Edition
  • Microsoft Visual C++ 5.0 Professional Edition
  • Microsoft Visual C++ 6.0 Professional Edition
  • Microsoft Visual C++ 6.0 Standard Edition
  • Microsoft Visual C++ .NET 2002 Standard Edition



This article was previously published under Q168385

SYMPTOMS

The "this" pointer is incorrectly set up in the destructor of a base class. This problem occurs when the array delete is called and there is a class that inherits from two base classes that have virtual functions and the second base class has a virtual destructor.

RESOLUTION

One workaround is to make the base classes virtual base classes. Another option is to use something other than array new and array delete to allocate and deallocate the memory. For example, declare the array statically.

STATUS

Microsoft has confirmed that this is a bug in the Microsoft products that are listed at the beginning of this article. This bug was corrected in Microsoft Visual C++ .NET (2003).

MORE INFORMATION

The following is example code:

   #include <iostream>
       using namespace std;
       class A {
       public:
          A(){}
          ~A(){}
          virtual void A1() {}
       };

       class B {
       public:
          B();
          virtual ~B();
          int marker;
       };

       B::B() {
          marker = 10;
          cout << "In constructor, this is " << this << endl;
       }

       B::~B() {
          cout << "In destructor, this is " << this << endl;
          if ( this->marker != 10)
             cout << "Data has been corrupted" << endl;
       }

       class C: public A, public B
       // class C: public A, virtual public B // Workaround
       {public:
          C(){}
          ~C(){}
       };

       int main() {
          C * pC;
          pC = new C[2];
          delete [] pC;
               return 0 ;
       }

   /* Sample Program Output */ 
   In constructor, this is 00460DA8
   In constructor, this is 00460DB4
   In destructor, this is 00460DB0
   Data has been corrupted
   In destructor, this is 00460DA4
   Data has been corrupted
                

Keywords: kbfix kbbug kbcompiler KB168385