@autoreleasepool 语义
我正在 llvm 网站上阅读 ARC 文档: http://clang.llvm.org/docs/AutomaticReferenceCounting.html #autoreleasepool
..特别是@autoreleasepool。
在当前使用 NSAutoreleasePool 的许多实现中,我看到池在循环迭代期间定期耗尽的情况 - 我们如何对 @autorelease pool 做同样的事情,或者这一切都是在幕后为我们完成的?
其次,文档指出,如果引发异常,池不会被耗尽......好吧,异常是从名称上来说是异常的,但如果它们确实发生了,您可能希望在不泄漏内存负载的情况下进行恢复。文档没有指定这些对象何时被释放。
有人知道有关这些点的任何信息吗?
I was reading the ARC docs on the llvm site: http://clang.llvm.org/docs/AutomaticReferenceCounting.html#autoreleasepool
..in particular about @autoreleasepool.
In lot of current implementation using NSAutoreleasePool, I see cases where the pool is drained periodically during a loop iteration - how do we do the same with @autorelease pool, or is it all done for us somehow under the hood?
Secondly, the docs state that if an exception is thrown, the pool isn't drained.... ok exceptions are by name exceptional, but if they do happen, you might like to recover without leaking a load of memory. The docs don't specify when these objects will be released.
Anyone got any info about these points?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
以同样的方式,即通过级联自动释放池。例如:
在大多数情况下,由于 Cocoa 中异常的特殊性质,程序将无法正常恢复,所以我认为泄漏对象是一个较小的问题。如果 @autoreleasepool 块由于异常而退出,则只有当其中一个封闭的自动释放池被弹出时,相应的自动释放对象才会被释放。当然,您可以将 @try/@catch/@finally 块放置在 @autoreleasepool 块内以防止这种情况发生。
In the same way, i.e., by cascading autorelease pools. For instance:
In most cases the program won’t be able to recover gracefully due to the peculiar nature of exceptions in Cocoa, so I’d say that leaking objects is a lesser problem. If an
@autoreleasepool
block is exited because of an exception, the corresponding autoreleased objects will only get released when one of the enclosing autorelease pools is popped. But you can, of course, place@try/@catch/@finally
blocks inside the@autoreleasepool
block to prevent this from happening.如下所示:
AFAIK 这是 ARC 不可能实现的。 ARC 根本不是异常安全的。如果发生异常,就有可能出现不可恢复的内存泄漏。使用 ARC 的代码不应依赖异常来报告错误。预期的是,当引发异常时,进程无论如何都会崩溃。
Like this:
AFAIK this is not possible with ARC. ARC is not exception-safe at all. If an exception occurs, there is the possibility of non-recoverable memory leaks. Code that uses ARC should not rely on exceptions for error reporting. The expectation is that the process crashes anyway when an exception is raised.