IOS 中 main()函数中UIApplicationMain后面的代码不执行...
来源:互联网 发布:破墙软件 编辑:程序博客网 时间:2024/05/22 09:43
下面外国友人给出了一定的结果:
Consider the following main() method which you’ll find in 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;
- }
You might assume (as I did) that when an iPhone application ends, the above call to UIApplicationMain() will return and the rest of main() will run, eventually returning an integer. That assumption would be incorrect, however.
UIApplicationMain() never actually gets a chance to finish, nor do any of the statements that follow it have a chance to execute. When an iPhone OS application is closed (e.g., the user pushes the home button) the application is given a few seconds to run its -applicationWillTerminate method and then it calls the exit() system function. This makes sense, if you think about it: it’s more efficient to just stop the program and let the operating system free up all of its memory at once instead of running several individual -dealloc statements. Dump the whole trash bin instead of picking stuff out one-by-one, so to speak.
However, when I noticed this several months ago (while trying to run some logging and shut-down statements in main) I was very confused; the official Apple documentation for the UIApplicationMain() function said zilch about UIApplicationMain() never returning. In fact, while all the iPhone documentation obviously encouraged putting shutdown code inside the standard -applicationWillTerminate method, it said nothing about the fact that it’s essentially pointless to write any code after your call to UIApplicationMain() (aside from the return statement, necessary to keep the compiler happy).
After some collective head-scratching on developer forums someone observed that the documentation for NSApplicationMain()–the AppKit/Cocoa counterpart to UIApplicationMain()–did describe the exit(0) behavior. This prompted me to fill out a quick bug report requesting that Apple update thedocumentation. Fast-forward to today, when I randomly came across that documentation and noticed that it has been updated and my bug was closed. Who knows if I’m actually the one responsible for instigating the correction, but I’ll tell myself that I am.
===========================邪恶的分割线===========================
好吧,我承认这是个BUG,下面有读者给出了一个方法:
Nevertheless, there are situations when it returns. For example, if you are raising an exception in applicationDidFinishLaunching and intercepting it in main you are reaching this point. Of course it’s non-standard way to run, but it may be considered in development as test-case in order to carefully log what happens.
int main(int argc, char *argv[])
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
int retVal = -1;
@try
{
retVal = UIApplicationMain(argc, argv, nil, nil);
}
@catch(NSException* ex)
{
NSLog(@”Application terminates unexpectedly. The reason is ‘ %@ ‘.”, ex.reason);
}
[pool release];
return retVal;
}
- IOS 中 main()函数中UIApplicationMain后面的代码不执行...
- iOS中UIApplicationMain函数
- Java中main函数后面带的参数(String[] args)
- android 在函数中使用return,让后面的代码不再执行
- iOS程序中main函数的作用
- 分享通过在java main函数中执行spring的代码
- iOS开发-UIApplicationMain 函数的功能
- ios程序入口main及UIApplicationMain的研究
- java中代码块的执行顺序--静态代码块,构造器,普通代码块,构造块,main主函数
- main中执行shellcode的一段代码的解释
- java中main函数后面带的参数(String[] args)是什么意思?
- Linux系统中,main函数的执行过程
- Linux系统中,main函数的执行过程
- Linux系统中main函数的执行过程
- Linux系统中,main函数的执行过程
- Linux系统中,main函数的执行过程
- Linux系统中,main函数的执行过程
- Linux系统中,main函数的执行过程
- IntelliJ idea 下如何使用spring2.5+log4j
- Java课程设计 大整数类 实现加法
- Linux全文查找、替换和删除
- js省市联动代码
- Memcache分组和同步机制的实现
- IOS 中 main()函数中UIApplicationMain后面的代码不执行...
- WPF程序设计指南第24张样式中DataTrigger中使用绑定的例子
- C# 新建文件夹、写文本文件
- 输出101~200内的质数
- vi编辑器不响应
- 智慧位置漫谈
- Java IO _RandomAccessFile 类
- 表结构数据字典---查询表结构明细信息
- 怎样使用AQL1234