如果我用“goto”跳出 catch 块,我是否能保证异常对象将被释放?

发布于 2024-12-02 03:35:31 字数 347 浏览 4 评论 0原文

我有如下代码

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

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

我的问题是,当我 goto 脱离 catch 块时,C++ 运行时存储的异常对象是否被释放?或者运行时是否允许缓存它,直到周围的函数存在或类似的东西?我只是想确保,如果我多次执行上述代码,每次执行倒带代码时,我都不会泄漏内存(因为 longjmp 不会执行编译器发出的清理代码到或在函数序言之前)。

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).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

瞳孔里扚悲伤 2024-12-09 03:35:31

§6.6/2:

退出作用域(无论如何完成)时,将为所有具有自动存储持续时间的构造对象调用析构函数(12.4)...

至少在我读到的时候,“无论如何完成”应该/确实包含一个 goto< /代码>。

编辑:好的,根据 Johannes 的评论,我们关心的是 §15.1/4:

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

[...]

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

§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.

空‖城人不在 2024-12-09 03:35:31

§15.1.4

异常对象的内存分配在未指定的位置
方式,除非 3.7.4.1 中注明。如果处理程序通过重新抛出退出,
对于同一异常,控制权被传递给另一个处理程序。
异常对象在最后剩余的活动之后被销毁
异常处理程序通过重新抛出以外的任何方式退出
,或者
std::exception_ptr (18.8.5) 类型的最后一个对象引用了
异常对象被销毁,以较晚者为准。在前一种情况下,
销毁发生在处理程序退出时,紧接着
销毁异常声明中声明的对象
处理程序(如果有)。
在后一种情况下,销毁发生在
std::exception_ptr 的析构函数返回。然后实施可以
释放异常对象的内存;任何此类重新分配
以未指定的方式完成。

§ 15.1.4

The memory for the exception object is allocated in an unspecified
way, except as noted in 3.7.4.1. If a handler exits by rethrowing,
control is passed to another handler for the same exception. The
exception object is destroyed after either the last remaining active
handler for the exception exits by any means other than rethrowing
, or
the last object of type std::exception_ptr (18.8.5) that refers to the
exception object is destroyed, whichever is later. In the former case,
the destruction occurs when the handler exits, immediately after the
destruction of the object declared in the exception-declaration in the
handler, if any.
In the latter case, the destruction occurs before the
destructor of std::exception_ptr returns. The implementation may then
deallocate the memory for the exception object; any such deallocation
is done in an unspecified way.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文