iOS 自动释放池 main 和 ARC 分配,释放

iOS 自动释放池 main 和 ARC 分配,释放

问题描述:

这可能是一个来自 iOS 新手的幼稚问题.

This is perhaps a naive question from iOS newbie.

我看到典型的 iOS 应用程序 main.m 有以下代码:

I see that typical iOS application main.m has following code:

int main(int argc, char * argv[]) {
    @autoreleasepool {
        return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));


    }
}

在 iOS 应用程序中围绕 UIApplicationMain 设置 autoreleasepool 有什么意义?UIApplicationMain 直到应用程序真正退出才会返回,所以为什么在 autoreleasepool 中有 UIApplicationMain(),它会为应用程序执行期间分配的所有对象触发空闲".

What is the point of having autoreleasepool around UIApplicationMain in iOS application? UIApplicationMain doesn't return till the application actually exits so why have UIApplicationMain() in autoreleasepool, which triggers 'free' for all the objects allocated during application's execution.

目前,使用 LLVM 编译器,每个线程默认都有一个自动释放池,这不是必需的.如果你使用不同的编译器和不同的自动释放池实现(它们现在是一个语言特性),你需要为每个线程设置一个自动释放池.调用永远不会返回无关紧要,必须设置池(并且可以通过它排空).

Currently, with LLVM compiler every thread has an autorelease pool by default and this is not needed. If you use a different compiler with a different implementation for autorelease pools (they are now a language feature), you are required to set up an autorelease pool for every thread. It doesn't matter that the call never returns, the pool has to be set up (and it can be drained thanks to it).

我找不到重复的问题,但我确定它在那里.

I can't find the duplicate question but I am sure it's there.