是main.m真正的地方,其中主运行循环的autorelease池是由每个事件创建的吗? [英] is the main.m really the place, where the autorelease pool of the main run loop is created by every event?

查看:158
本文介绍了是main.m真正的地方,其中主运行循环的autorelease池是由每个事件创建的吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#import <UIKit/UIKit.h>

int main(int argc, char *argv[]) {
    NSLog(@"new event...");
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    int retVal = UIApplicationMain(argc, argv, nil, nil);
    [pool release];
    return retVal;
}

如果是这样,那么main()函数必须被调用对每个事件,对吧?但我试了一下,新事件...日志消息只是在应用程序启动。所以我想在主线程中必须有另一个自动释放池。

If that's the case, then the main() function would have to be called on every event, right? But I tried it, and the "new event..." log message comes just on app start. So I guess that there must be another autorelease pool in the main thread.

推荐答案

否。所有Cocoa或CocoaTouch类都需要一个自动释放池,以便不泄漏内存。因此,需要现有的自动释放池来调用UIApplicationMain(),以便覆盖在UIApplicationMain()的上下文中实例化的任何(可能)自动释放对象。这个外部自动释放池是,正如你可以看到在UIApplicationMain返回后排空,就在应用程序退出之前。内部(记住自动释放池可以嵌套,自动释放对象被添加到最新/最深的池)autorelease池在应用程序运行循环的每次迭代开始时创建,并在迭代结束时释放。因此,运行循环的每次迭代都是自己的自动释放池。如果处理事件可能会产生大量的自动释放内存(在iPhone上是一个坏主意,但在OS X上很常见),您可能想在事件处理代码中创建自己的内部自动释放池,事件。

No. All Cocoa or CocoaTouch classes require the presence of an autorelease pool in order to not leak memory. Thus, an existing autorelease pool is required to call UIApplicationMain() in order to cover any (possibly) autoreleased objects that are instantiated in the context of UIApplicationMain(). This outer autorelease pool is, as you can see drained after return of UIApplicationMain, just before application exit. An inner (remember that autorelease pools can be nested and autoreleased objects are added to the newest/deepest pool) autorelease pool is created at the beginning of each iteration of the application's run loop and released at the end of the iteration. Thus, each iteration of the run loop gets is "own" autorelease pool. If processing an event might generate a lot of autoreleased memory (a bad idea on the iPhone, but quite common on OS X), you may want to create your own inner autorelease pools in the event handling code which can be released during processing of that event.

这篇关于是main.m真正的地方,其中主运行循环的autorelease池是由每个事件创建的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆