is the main.m really the place, where the autorelease pool of the main run loop is created by every event?

风格不统一 提交于 2019-12-06 11:37:50

问题


#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;
}

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.


回答1:


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.




回答2:


No, this is the outermost function in your application, a regular C-style main().

Everything that the iPhone app does takes place in UIApplicationMain, including all the event handling.



来源:https://stackoverflow.com/questions/798950/is-the-main-m-really-the-place-where-the-autorelease-pool-of-the-main-run-loop

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!