Question

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?

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top