Microsoft KB Archive/168936: Difference between revisions
(importing KB archive) |
m (Text replacement - "<" to "<") |
||
Line 83: | Line 83: | ||
<pre class="codesample"> //compiler options needed /GX | <pre class="codesample"> //compiler options needed /GX | ||
#include | #include <iostream.h> | ||
class A | class A | ||
{ | { | ||
public: | public: | ||
A() { cout | A() { cout << "In A-ctor" << endl;} | ||
~A(){ cout | ~A(){ cout << "In A-dtor" << endl;} | ||
}; | }; | ||
Line 94: | Line 94: | ||
{ | { | ||
public: | public: | ||
B1() { cout | B1() { cout << "In B1-ctor" << endl;} | ||
~B1(){ cout | ~B1(){ cout << "In B1-dtor" << endl;} | ||
}; | }; | ||
Line 101: | Line 101: | ||
{ | { | ||
public: | public: | ||
B2() { cout | B2() { cout << "In B2-ctor" << endl;} | ||
~B2(){ cout | ~B2(){ cout << "In B2-dtor" << endl;} | ||
}; | }; | ||
Line 109: | Line 109: | ||
public: | public: | ||
C(){ | C(){ | ||
cout | cout << "In C-ctor...throw Exception" << endl; | ||
throw int(1); //incorrect destructor calls... | throw int(1); //incorrect destructor calls... | ||
} | } | ||
~C(){ cout | ~C(){ cout << "In C-dtor" << endl;} | ||
}; | }; | ||
Line 125: | Line 125: | ||
catch (int ex) | catch (int ex) | ||
{ | { | ||
cout | cout << "Caught Exception #" << ex << endl; | ||
} | } | ||
} | } |
Revision as of 17:00, 20 July 2020
Article ID: 168936
Article Last Modified on 7/5/2005
APPLIES TO
- Microsoft Visual C++ 5.0 Professional Edition
- Microsoft Visual C++ 5.0 Enterprise Edition
- Microsoft Visual C++ 4.2 Professional Edition
- Microsoft Visual C++ 4.2 Enterprise Edition
- Microsoft Visual C++ 4.1 Subscription
- Microsoft Visual C++ 4.0 Standard Edition
This article was previously published under Q168936
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.
For additional information about the two-phased construction, please see the following article in the Microsoft Knowledge Base:
132893 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. This problem was corrected in Visual C++ version 6.0 for Windows.
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; } }
The 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: kbbug kbfix kbvc600fix kbcpponly kbcompiler KB168936