FIX: Destructor Called on Non Constructed TemporaryLast reviewed: September 18, 1997Article ID: Q129617 |
1.50 1.51 1.52 | 1.00 2.00 2.10
WINDOWS | WINDOWS NTkbtool kbfixlist kbprg The information in this article applies to:
SYMPTOMSThe destructor of a class is called on a temporary that was never constructed if all the following conditions exist:
WORKAROUNDListed below in order of preference are three workarounds to this problem:
STATUSMicrosoft 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 INFORMATIONThe 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
© 1998 Microsoft Corporation. All rights reserved. Terms of Use. |