FIX: Destructor Called on Non Constructed Temporary

Last reviewed: September 18, 1997
Article ID: Q129617
1.50 1.51 1.52 | 1.00 2.00 2.10
WINDOWS        | WINDOWS NT
kbtool kbfixlist kbprg

The information in this article applies to:

  • The Microsoft C/C++ Compiler (CL.EXE) included with:

        - Microsoft Visual C++ for Windows, versions 1.5, 1.51, and 1.52
        - Microsoft Visual C++, 32-bit Edition, versions 1.0, 2.0, and 2.1
    

SYMPTOMS

The destructor of a class is called on a temporary that was never constructed if all the following conditions exist:

  • Two classes A and B are created.
  • A global function takes an argument of class B by value.
  • A conversion operator is used to convert from class A to class B.
  • The global function is called with an item of class A.

The destructor being called for an object that was never constructed can cause a general protection (GP) fault or other memory errors at run time.

WORKAROUND

Listed below in order of preference are three workarounds to this problem:

  • Define a conversion constructor in class B instead of a conversion operator in class A.

    -or-

  • Define the function to take an argument of "const B &" instead of taking class B by value.

    -or-

  • Explicitly instantiate a B object and call the global function with the instance of the B object.

The first workaround produces much cleaner code than the other two workarounds. See the sample code listed below for further details.

STATUS

Microsoft has confirmed this to be a bug in the Microsoft products listed at the beginning of this article. This problem was fixed in Microsoft Visual C++, 32-bit Edition, version 4.0.

MORE INFORMATION

The sample code below can be used to reproduce this problem. If the program is complied with no options, the following output is displayed when the program is run:

   -- A ctor called
   -- Class A -> Class B conversion called
   -- B ctor called, Created B: 1245064
   -- B copy ctor, Created B: 1245096
   -- B dtor called, Destroyed B: 1245064
   -- Calling function test(B) on B: 1245076
   -- B dtor called, Destroyed B: 1245076
       This B was never constructed!
   -- B dtor called, Destroyed B: 1245096
   -- A dtor called

Sample Code to Reproduce Problem

/* Compile options needed, choose one of the following:
          none           - To demonstrate the problem.
          /DWORKAROUND1  - For workaround 1.
          /DWORKAROUND2  - For workaround 2.
          /DWORKAROUND3  - For workaround 3.
*/

#include <iostream.h>

class A;

class B {

   B *pBthis;
public:
   B() {
      pBthis = this;
      cout << "-- B ctor called, Created B: " << (long)this << endl;
   }
#ifdef WORKAROUND1
   B(const A&) {
      pBthis = this;
      cout << "-- B(A) ctor called, Created B: " << (long)this << endl;
   }
#endif
   B( const B& b ) {
      pBthis = this;
      cout << "-- B copy ctor, Created B: " << (long)this << endl;
   }
   ~B() {
      cout << "-- B dtor called, Destroyed B: " << (long)this << endl;
     if (pBthis != this)
         cout << "    This B was never constructed!" << endl;
   }
};

class A { public:

   A() { cout << "-- A ctor called" << endl; }
   ~A() { cout << "-- A dtor called" << endl; }

#ifndef WORKAROUND1
   operator B() {
      cout << "-- Class A -> Class B conversion called" << endl;
      B b1;
      return b1;
   }
#endif

};

#ifdef WORKAROUND2

void test(const B &b)
#else
void test(B b)
#endif {
   cout << "-- Calling function test(B) on B: " << (long)(&b) << endl;
}

int main()
{
   A a;
#ifdef WORKAROUND3
   B b(a);
   test(b);
#else
   test(a);
#endif
   return 0;
}


Additional reference words: 1.00 1.50 1.51 1.52 2.00 2.10 8.0 8.00 8.0c
8.00c 9.0 9.00 GPF
KBCategory: kbtool kbfixlist kbbuglist kbprg
KBSubCategory: CPPIss
Keywords : kbbuglist kbfixlist kbprg kbtool
Version : 1.50 1.51 1.52 | 1.00 2.00 2.10
Platform : NT WINDOWS
Solution Type : kbfix


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: September 18, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.