0

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 Answer 1

4

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.

2
  • i check out the past post on stackoverlow , relating to my question. it states there is bug existence long time ago. Commented Jan 13, 2013 at 15:47
  • I'm not certain what you mean here. There's no bug. It behaves as documented. -run is not guaranteed to return.
    – Rob Napier
    Commented Jan 13, 2013 at 21:48

Not the answer you're looking for? Browse other questions tagged or ask your own question.