如果我从“goto”跳出一个catch块,我保证异常对象将被释放吗? [英] If I jump out of a catch-block with "goto", am I guaranteed that the exception-object will be free'ed?

查看:297
本文介绍了如果我从“goto”跳出一个catch块,我保证异常对象将被释放吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的代码如下

  try {
doSomething();
} catch(InterruptException){
goto rewind_code;
}

if(0){
rewind_code:
longjmp(savepoint,1);
}

我的问题是,是由C ++运行时免费存储的异常对象当我 goto 出来的catch块?或者是允许缓存它的运行时间,直到周围的函数存在或类似的东西?我只是想确保如果我多次执行上面的代码,每次采取倒带代码,我不会泄漏内存(因为 longjmp 不会执行清除代码由编译器发送到函数序言之前或之前)。

解决方案

§6.6/ 2:


从范围退出(无论如何),对所有自动存储持续时间的构造对象调用析构函数(12.4)...


至少如我所看到的那样,完成应该包含一个 goto



编辑:好的,根据约翰内斯的评论,我们关心的是§15.1/ 4:


当最后一个处理程序被执行为
异常以除throw之外的任何方式退出;临时对象被销毁,执行
可能释放临时对象的内存;


[...]


在处理程序中的异常声明
中声明的对象销毁后立即发生破坏。



I have such code as follows

try {
  doSomething();
} catch(InterruptException) {
  goto rewind_code;
}

if(0) {
rewind_code:
  longjmp(savepoint, 1);
}

My question is, is the exception object that is stored by the C++ runtime free'ed when I goto out of the catch block? Or is the runtime allowed to cache it until the surrounding function exists or something like that? I simply want to ensure that if I execute above code multiple times, each time taking the rewind code, I won't leak memory (because the longjmp won't execute cleanup code emitted by the compiler into or before function prologues).

解决方案

§6.6/2:

On exit from a scope (however accomplished), destructors (12.4) are called for all constructed objects with automatic storage duration...

At least as I'd read it, "however accomplished" should/does include a goto.

Edit: Okay, based on Johannes's comment, what we care about is §15.1/4:

When the last handler being executed for the exception exits by any means other than throw; the temporary object is destroyed and the implementation may deallocate the memory for the temporary object;

[ ... ]

The destruction occurs immediately after the destruction of the object declared in the exception-declaration in the handler.

这篇关于如果我从“goto”跳出一个catch块,我保证异常对象将被释放吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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