为什么在执行“[池流失]”之后,它从不回到呼叫者? [英] why after executing "[pool drain]", it never return back to caller?

查看:105
本文介绍了为什么在执行“[池流失]”之后,它从不回到呼叫者?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里是我的代码模板:

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 。

"[pool drain]" followd by "return 0" why never get executed.

但是,我发现另一个gnustep官方例子,它也做同样的事情。

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

以证明它[theApp run]永远不会回来,在[theApp run]之后立即添加一个无限循环的做法,但它从来不被执行。为什么从GNUSTEP官方的例子做这样的?

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?

推荐答案

你确定 实际上是被调用吗? [应用程序运行] 将不会返回,除非调用 [NSApp stop] 如果调用更常见的 [NSApp terminate] ,因为文档说

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:


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

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.

code>运行,你一般不会得到它。

Once you hand the application over to run, you generally aren't going to get it back.

这篇关于为什么在执行“[池流失]”之后,它从不回到呼叫者?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆