objective-c - 为什么执行 "[pool drain]"后,它永远不会返回给调用者?

标签 objective-c cocoa gnustep

这是我的代码模板:

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

“[池耗尽]”后跟“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官方的例子要这样做?

最佳答案

您确定[pool排出]实际上被调用了吗? [application run] 不会返回,除非调用 [NSApp stop](这种情况很少见)。如果调用更常见的[NSApp终止],如 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.

一旦您将应用程序移交给运行,您通常就不会再将其取回。

关于objective-c - 为什么执行 "[pool drain]"后,它永远不会返回给调用者?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14290039/

相关文章:

ios - iPhone 4s (8.4.1) 的 linphone 库中的 Sigabart 错误

iphone - 是否可以使用 Accelerate/LAPACK 求解非方欠/过约束矩阵?

objective-c - 输入字符串在静态方法中设置后为空,为什么?

objective-c - 可用于设置计算机 sleep 并在 mac os x 中的 cocoa 应用程序中显示 sleep 值的 API

ios - 着色/绘图代码。如何以较低的不透明度使其更光滑?去除刷点?

使用适用于 iOS 的 Google Maps SDK 的 iOS 应用程序崩溃并出现异常代码 : 0x0000000000000000

ios - 自动布局错误

objective-c - Objective C 项目组织

objective-c - 无法在 Windows 上使用 Gnustep 编译 Objective C

cocoa - 基金会使用核心基金会吗?