[英]Constructor finishes by throwing an exception ? Is there a memory leak?
我正在审阅这篇文章,并指出
注意:如果构造函数通过抛出异常结束,则清除与对象本身关联的内存 - 没有内存泄漏。 例如:
void f()
{
X x; // If X::X() throws, the memory for x itself will not leak
Y* p = new Y(); // If Y::Y() throws, the memory for *p itself will not leak
}
我很难理解这一点,如果有人能澄清这一点,我会很感激。 我尝试了下面的示例,该示例显示了构造函数中的异常,不会调用析构函数。
struct someObject
{
someObject()
{
f = new foo();
throw 12;
}
~someObject()
{
std::cout << "Destructor of someobject called";
}
foo* f;
};
class foo
{
public:
foo()
{
g = new glue();
someObject a;
}
~foo()
{
std::cout << "Destructor of foo";
}
private:
glue* g;
};
int main()
{
try
{
foo a;
}
catch(int a)
{
//Exception caught. foo destructor not called and someobject destrucotr not called.
//Memory leak of glue and foo objects
}
}
我该如何解决这个问题?
很抱歉,更新可能给您带来不便。
“......不会召唤析构函数。”
由于该对象尚未被视为已构造,因此在构造函数因异常而失败后,将不会调用析构函数。
对象分配和构造(仅仅是破坏)是不同的东西。
在抛出异常之前使用new()
分配的任何对象都将泄漏。
你不应该自己管理这些资源,除非你真的,真的,真的需要它,并且对你正在做的事情100%肯定。
而是为标准动态内存管理库中的类成员使用合适的智能指针。
当构造函数抛出时,不会调用析构函数。 当在构造函数中抛出异常时,在正确处理可能在对象的中止构造中发生的资源分配方面,您应该注意以下几点:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.