提前返回/退出时 @autoreleasepool {} 是否会耗尽?

发布于 2025-01-04 03:25:19 字数 330 浏览 5 评论 0原文

考虑这个例子:

- (void)doSomething {
   @autoreleasepool {
      if (someCondition) {

         /* ... allocate some autoreleased objects here ... */

         return;
      }
   }
}

以前,使用手动 NSAutoreleasePools,如果我们提前返回,我们需要调用 [pool排出],否则池将不会被排出。 使用新的 @autoreleasepool {}

Consider this example:

- (void)doSomething {
   @autoreleasepool {
      if (someCondition) {

         /* ... allocate some autoreleased objects here ... */

         return;
      }
   }
}

Previously, with manual NSAutoreleasePools, if we returned early, we needed to call [pool drain], otherwise the pool would not be drained.
With the new @autoreleasepool {}

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

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

发布评论

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

评论(1

一曲琵琶半遮面シ 2025-01-11 03:25:19

答案是肯定的:

当块正常退出时,无论是通过fallthrough还是定向控制流(例如return或break),自动释放池都会恢复到保存的状态,释放其中的所有对象。当块因异常退出时,池不会被耗尽。

来源:
http://clang.llvm.org/docs/AutomaticReferenceCounting.html#autoreleasepool

The answer is YES:

When the block is exited normally, whether by fallthrough or directed control flow (such as return or break), the autorelease pool is restored to the saved state, releasing all the objects in it. When the block is exited with an exception, the pool is not drained.

Source:
http://clang.llvm.org/docs/AutomaticReferenceCounting.html#autoreleasepool

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