why after executing “[pool drain]”, it never return back to caller?

那年仲夏 提交于 2019-12-25 09:44:40

问题


here is my code template:

int main(int argc, char *argv[]) {
    // create an autorelease pool
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    // make sure the application singleton has been instantiated
    NSApplication * application = [NSApplication sharedApplication];
    // instantiate our application delegate
    AppDelegate * applicationDelegate =
                          [[[AppDelegate alloc] init] autorelease];
    // assign our delegate to the NSApplication
    [application setDelegate:applicationDelegate];
    // call the run method of our application
    [application run];
    // drain the autorelease pool
    [pool drain];
    // execution never gets here ..
    return 0;
}

"[pool drain]" followd by "return 0" why never get executed.

but, i found another one of gnustep official examples, it does the same thing.

int
main(int argc, char **argv, char** env)
{
  id pool = [NSAutoreleasePool new];
  NSApplication *theApp;

#if LIB_FOUNDATION_LIBRARY
  [NSProcessInfo initializeWithArguments:argv count:argc environment:env];
#endif

  theApp = [NSApplication sharedApplication];
  [theApp setDelegate: [browserController new]];
  [theApp run];

  [pool release];

  return 0;
}

to prove it "[theApp run]" never get back,i have done the practice with adding an infinite loop immediately after " [theApp run]".but it never get executed. why did the example from GNUSTEP official do such this?


回答1:


Are you certain that [pool drain] is actually getting called either? [application run] won't return unless [NSApp stop] is called (which is rare). If the more common [NSApp terminate] is called, as the docs say:

Do not bother to put final cleanup code in your application’s main() function—it will never be executed. If cleanup is necessary, perform that cleanup in the delegate’s applicationWillTerminate: method.

Once you hand the application over to run, you generally aren't going to get it back.



来源:https://stackoverflow.com/questions/14290039/why-after-executing-pool-drain-it-never-return-back-to-caller

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