Article ID: 168936
Article Last Modified on 7/5/2005
132893 PRB: Exceptions Thrown During Construction Can Orphan Memory
//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;
}
}
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
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