微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

objective-c – @autoreleasepool语义

我在llvm网站上阅读ARC文档: http://clang.llvm.org/docs/AutomaticReferenceCounting.html#autoreleasepool

..尤其是@autoreleasepool.

在使用NSAutoreleasePool的许多当前实现中,我看到在循环迭代期间池被定期耗尽的情况 – 我们如何对@autorelease池执行相同的操作,或者这些都是以某种方式为我们完成的?

其次,文档声明如果抛出异常,池就不会耗尽……好的例外是名称异常,但如果它们确实发生了,你可能希望恢复而不会泄漏大量内存.文档未指定何时释放这些对象.

任何人都有关于这些点的任何信息?

解决方法

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?

以相同的方式,即通过级联自动释放池.例如:

@autoreleasepool {
    …
    for (int i = 0; i < MAX; i++) {
        @autoreleasepool {
            …
        }
    }
    …
}

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.

在大多数情况下,由于Cocoa中异常的特殊性,程序将无法正常恢复,因此我认为泄漏对象是一个较小的问题.如果由于异常而退出@autoreleasepool块,则只有在弹出其中一个封闭的自动释放池时才会释放相应的自动释放对象.但是,你当然可以在@autoreleasepool块中放置@ try / @ catch / @ finally块来防止这种情况发生.

原文地址:https://www.jb51.cc/c/119676.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐