gpt4 book ai didi

c++ - 如果我跳出带有 "goto"的捕获 block ,我是否保证异常对象将被释放?

转载 作者:IT老高 更新时间:2023-10-28 22:59:31 26 4
gpt4 key购买 nike

我有如下代码

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

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

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

最佳答案

§6.6/2:

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

至少正如我所读到的那样,“无论如何完成”应该/确实包含一个 goto

编辑:好的,根据 Johannes 的评论,我们关心的是 §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.

关于c++ - 如果我跳出带有 "goto"的捕获 block ,我是否保证异常对象将被释放?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7262818/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com