BUG: Virtual Base Class Destructor Called More than Once

Last reviewed: July 31, 1997
Article ID: Q168936
The information in this article applies to:
  • The C/C++ Compiler (CL.EXE) included with: - Microsoft Visual C++, 32-bit Editions, versions 4.0, 4.1, 4.2, 5.0

SYMPTOMS

If an exception is thrown from the constructor of a class that is derived from a virtual base class, the destructor for the virtual base class is called more than once. This doesn't happen if the exception is thrown from any other member function.

RESOLUTION

There is no workaround. Avoid throwing exceptions from the constructor. Use the two-phased construction documented in the following article in the Microsoft Knowledge Base:

   ARTICLE-ID: Q132893
   TITLE     : PRB: Exceptions Thrown During Construction Can Orphan Memory

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.

MORE INFORMATION

Steps to Reproduce Behavior

   //compiler options needed  /GX

    #include <iostream.h>
    class A
    {
      public:
            A() { cout << "In A-ctor" << endl;}
            ~A(){ cout << "In A-dtor" << endl;}
    };

    class B1 : public  virtual   A
    {
      public:
              B1() { cout << "In B1-ctor" << endl;}
            ~B1(){ cout << "In B1-dtor" << endl;}
    };

    class B2 : public  virtual  A
    {
      public:
              B2() { cout << "In B2-ctor" << endl;}
            ~B2(){ cout << "In B2-dtor" << endl;}
    };

    class C : public B1 ,public B2
    {
      public:
          C(){
         cout << "In C-ctor...throw Exception" << endl;
         throw int(1); //incorrect destructor calls...
             }

          ~C(){ cout << "In C-dtor" << endl;}
    };

    void main()
    {
      try
       {
         C c;

       }
      catch (int ex)
       {
         cout << "Caught Exception #" << ex << endl;
         }
    }

    Program Output is :
    In A-ctor
    In B1-ctor
    In B2-ctor
    In C-ctor...throw Exception
    In B2-dtor
    In A-dtor
    In B1-dtor
    In A-dtor
    In A-dtor
    Caught Exception #1


    The Expected Output is:
    In A-ctor
    In B1-ctor
    In B2-ctor
    In C-ctor...throw Exception
    In B2-dtor
    In B1-dtor
    In A-dtor
    Caught Exception #1
Keywords          : CPPIss vcbuglist500 kbtool
Version           : 4.0 4.1 4.2 5.0
Platform          : NT WINDOWS
Issue type        : kbbug


================================================================================


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: July 31, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.