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块来防止这种情况发生.

相关文章

本程序的编译和运行环境如下(如果有运行方面的问题欢迎在评...
水了一学期的院选修,万万没想到期末考试还有比较硬核的编程...
补充一下,先前文章末尾给出的下载链接的完整代码含有部分C&...
思路如标题所说采用模N取余法,难点是这个除法过程如何实现。...
本篇博客有更新!!!更新后效果图如下: 文章末尾的完整代码...
刚开始学习模块化程序设计时,估计大家都被形参和实参搞迷糊...