【问题标题】:why after executing "[pool drain]", it never return back to caller?为什么在执行“[pool drain]”之后,它永远不会返回给调用者?
【发布时间】:2013-01-12 03:50:45
【问题描述】:

这是我的代码模板:

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]”后跟“return 0”为什么永远不会被执行。

但是,我找到了另一个 gnustep 官方示例,它做同样的事情。

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

为了证明“[theApp run]”永远不会回来,我已经完成了在“[theApp run]”之后立即添加无限循环的练习。但它永远不会被执行。为什么 GNUSTEP 官方的例子会这样呢?

【问题讨论】:

    标签: objective-c cocoa gnustep


    【解决方案1】:

    您确定[pool drain] 实际上也被调用了吗? [application run] 不会返回,除非 [NSApp stop] 被调用(这种情况很少见)。如果调用更常见的[NSApp terminate],为the docs say

    不要费心将最终的清理代码放在应用程序的 main() 中 函数——它永远不会被执行。如果需要清理,请执行 在委托的 applicationWillTerminate: 方法中进行清理。

    一旦您将申请交给run,您通常不会再取回它。

    【讨论】:

    • 我查看了 stackoverlow 上与我的问题相关的过去帖子。它表明很久以前就存在错误。
    • 我不确定你的意思。没有错误。它的行为与记录的一样。 -run 不保证返回。
    猜你喜欢
    • 2019-12-21
    • 1970-01-01
    • 1970-01-01
    • 2018-11-12
    • 2016-02-08
    • 1970-01-01
    • 2022-12-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多