析构函数? [英] Destructor??

查看:129
本文介绍了析构函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




有没有人有破坏者抛出执行的场景,这将导致内存泄漏......这是否有意义..


振作起来..

解决方案



Radde写道:< blockquote class =post_quotes> HI,

有没有人有景点,析构函数会抛出导致内存泄漏的行为......这是否有意义..
干杯......



希望不是。析构函数永远不应该抛出。


/ Peter


Radde写道:



有没有人有景点,析构函数会引发内存泄漏的行为......这是否有意义......

干杯......



http://www.parashift.com/c++-faq-lit....html#faq-17.3


析构函数不得抛出异常。更确切地说,永远不要允许

例外离开析构函数。它可能导致内存泄漏。考虑以下代码

...


模板< typename Iter>

void destroy(Iter begin,Iter end)

{

while(begin!= end)

{

// destruct object(* begin)

++开始;

}

}


如果(* begin)属于T类型并且在循环中,第一个析构函数抛出,然后该函数将退出,其他对象将永远不会被销毁。这只是一个粗略的例子,表明抛出的b / b $ b $的析构函数很糟糕并且可能导致泄漏。


问候,

Srini


HI,

Do anyone have scenrio where destructor throws execption which will
lead to memory leak..Does this make sense..

Cheers up..

解决方案


Radde wrote:

HI,

Do anyone have scenrio where destructor throws execption which will
lead to memory leak..Does this make sense..

Cheers up..


Hopefully not. A destructor should never throw.

/Peter


Radde wrote:

HI,

Do anyone have scenrio where destructor throws execption which will
lead to memory leak..Does this make sense..

Cheers up..



http://www.parashift.com/c++-faq-lit....html#faq-17.3


Destructors must NOT throw exceptions. More precisely, never allow
exceptions to leave destructors. It can lead to memory leaks. Consider
the following code...

template <typename Iter>
void destroy(Iter begin, Iter end)
{
while(begin != end)
{
// destruct object (*begin)
++begin;
}
}

If (*begin) is of type T, and in the loop, the first destructor throws,
then the function will exit and the other objects will never be
destroyed. This was just a crude example to show that destructors that
throw are bad and can cause leaks.

Regards,
Srini


这篇关于析构函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆