@autoreleasepool 语义

发布于 2024-12-10 15:29:29 字数 462 浏览 5 评论 0原文

我正在 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 技术交流群。

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

发布评论

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

评论(2

花开浅夏 2024-12-17 15:29:29

在当前使用 NSAutoreleasePool 的许多实现中,我看到在循环迭代期间定期耗尽池的情况 - 我们如何对 @autorelease pool 做同样的事情,或者这一切都是在幕后为我们完成的? p>

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

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

其次,文档指出,如果抛出异常,池不会被耗尽......好吧,异常从名称上讲是异常,但如果它们确实发生,您可能希望在不泄漏内存负载的情况下进行恢复。文档没有指定何时释放这些对象。

在大多数情况下,由于 Cocoa 中异常的特殊性质,程序将无法正常恢复,所以我认为泄漏对象是一个较小的问题。如果 @autoreleasepool 块由于异常而退出,则只有当其中一个封闭的自动释放池被弹出时,相应的自动释放对象才会被释放。当然,您可以将 @try/@catch/@finally 块放置在 @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?

In the same way, i.e., by cascading autorelease pools. For instance:

@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.

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.

一笔一画续写前缘 2024-12-17 15:29:29

我们如何对@autorelease池做同样的事情

如下所示:

for (int i = 0; i < 10000; i++) {
    @autoreleasepool {
        // Do your work here
        ...
    }
}

其次,文档指出,如果抛出异常,池不会被耗尽......好吧,异常从名称上讲是异常,但如果它们确实发生,您可能希望在不泄漏内存负载的情况下进行恢复。

AFAIK 这是 ARC 不可能实现的。 ARC 根本不是异常安全的。如果发生异常,就有可能出现不可恢复的内存泄漏。使用 ARC 的代码不应依赖异常来报告错误。预期的是,当引发异常时,进程无论如何都会崩溃。

how do we do the same with @autorelease pool

Like this:

for (int i = 0; i < 10000; i++) {
    @autoreleasepool {
        // Do your work here
        ...
    }
}

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.

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.

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