为什么iPhone应用程序的main()函数没有机会完成? [英] Why doesn't an iPhone app's main() function ever get a chance to finish?

查看:57
本文介绍了为什么iPhone应用程序的main()函数没有机会完成?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑以下大多数iPhone应用程序可以使用的main()方法:

Consider the following main() method which you would find most iPhone applications:

int main(int argc, char *argv[])
{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    int retVal = UIApplicationMain(argc, argv, nil, nil);
    [pool release];
    return retVal;
}

在我用它们在Simulator中运行的每个iPhone应用程序中(包括Apple提供的几个示例项目),线程都不会退出UIApplicationMain(),并且永远不会执行main()中的任何剩余代码.这是预期的行为吗?

In every iPhone app that I've run in Simulator with these (including several sample projects provided by Apple), the thread never exits UIApplicationMain() and any remaining code in main() is never executed. Is this expected behavior?

我已经通过调试器单步执行代码来验证UIApplicationMain()之后的语句永远不会运行.当用户停止应用程序时(例如,通过单击主页"按钮),生成的堆栈跟踪显示最终调用了[UIApplication _terminateWithStatus:].此函数调用您的应用程序委托的applicationWillTerminate:方法.一旦完成,[UIApplication _terminateWithStatus:]似乎会杀死/退出线程.

I have verified that statements after UIApplicationMain() never run by stepping through the code with a debugger. When the user stops an application (by hitting the "Home" button, for example), the resulting stack trace shows that [UIApplication _terminateWithStatus:] is eventually called. This function calls your application delegate's applicationWillTerminate: method. Once that finishes, [UIApplication _terminateWithStatus:] seems to kill/exit the thread.

有人可以确认这是main()应该工作的方式,还是至少在他们的计算机上确认相同的行为?

Can someone confirm that this is how main() is supposed to work, or at least confirm the same behavior on their machine?

推荐答案

最初的问题是:"为什么iPhone应用程序的main()函数没有机会完成?"

简短的回答:因为UIApplicationMain()的编码方式使其永不返回.

在Simulator和设备上进行了几次测试之后,并要求另一位开发人员进行相同的测试,我确认UIApplicationMain从不返回.当用户通过单击主页"按钮正常终止应用程序时,该程序最终终止于一个未发布的名为_terminateWithStatus的UIApplication方法中.此方法调用exit(0).

After doing several tests in Simulator and on the device, and asking another developer to do the same tests, I have confirmed that UIApplicationMain never returns. When the user terminates an application normally by hitting the Home button, The program ultimately terminates inside an unpublished UIApplication method called _terminateWithStatus. This method calls exit(0).

此行为与NSApplicationMain函数(UIApplicationMain函数的AppKit/Cocoa版本)的行为匹配. NSApplicationMain()的文档明确指出它将永远不会返回.

This behavior matches that of the NSApplicationMain function (which is AppKit/Cocoa version of the UIApplicationMain function). The documentation for NSApplicationMain() clearly states that it will never return.

我已向Apple提交了一个错误(6600198),要求更正官方文档(和main.m的Xcode模板)以声明UIApplicationMain()永不返回.虽然这不是功能性问题,但当前的模板和文档具有误导性.

I have submitted a bug (6600198) to Apple requesting that the official documentation (and Xcode template for main.m) be corrected to state that UIApplicationMain() never returns. While this is not a functional problem, the current template and docs are misleading.

感谢大家的所有投入和集思广益!

Thanks to everyone for all the input and brainstorming!

这篇关于为什么iPhone应用程序的main()函数没有机会完成?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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