gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 17:31:50 24 4
gpt4 key购买 nike

这是我的代码模板:

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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com