Abh*_*hay 2 c++ destructor exception
我们有一个类的语义行为如下: -
struct Sample
{
~Sample() throw()
{
throw 0;
}
};
void f ()
{
try
{
delete new Sample;
}
catch (...){
}
}
Run Code Online (Sandbox Code Playgroud)
我知道在dtors中抛出异常是邪恶的; 但放弃第三方图书馆资源是一个例外(但可以立即重新获得,有些奇怪!).还有一个这个资源的池,比如一个Sample类的数组/容器.因此,有两种情况需要考虑:破坏动态分配的对象和破坏动态分配的对象数组.
目前,只有在使用阵列版本(池)时,应用程序才会在不同的执行点随机崩溃.我们认为这是由于内存损坏,但那么为什么unpooled版本有效呢?
分配的内存会发生什么?是不确定的行为?在阵列的情况下会发生什么?是否调用了数组所有元素的dtors(至少,而不是内存)(比如第一个元素的dtor是否被抛出)?
提前致谢,
EDIT-1:嗯,我们跟踪了一些没有被调用的数组元素.但分配的内存似乎没有问题......以下是SC22-N-4411.pdf的第5.3.5.7节)
If the value of the operand of the delete-expression is not a null pointer value, the delete-expression will
call a deallocation function (3.7.4.2). Otherwise, it is unspecified whether the deallocation function will be
called. [ Note: The deallocation function is called regardless of whether the destructor for the object or some
element of the array throws an exception. —end note ]
Run Code Online (Sandbox Code Playgroud)
<\剪断>
看起来在这种情况下总是释放内存.我正确地解释标准吗?
| 归档时间: |
|
| 查看次数: |
882 次 |
| 最近记录: |