我正在和一些同事讨论当你在动态分配的类中抛出异常时会发生什么。我知道它malloc
会被调用,然后是类的构造函数。构造函数永远不会返回,那么会发生什么malloc
?
考虑以下示例:
class B
{
public:
B()
{
cout << "B::B()" << endl;
throw "B::exception";
}
~B()
{
cout << "B::~B()" << endl;
}
};
void main()
{
B *o = 0;
try
{
o = new B;
}
catch(const char *)
{
cout << "ouch!" << endl;
}
}
malloced 内存会发生什么o
,它会泄漏吗?CRT 是否捕获构造函数的异常并释放内存?
干杯!
富有的