我只写些相对来说,比较有用的信息,想完整阅读的,可以自行去原文。。
原文来自:
学习如何Debug和处理App闪退
处理闪退不一定很难,除非你精神贲溃并且胡乱地更改代码,期望这个bug会奇迹般的消失。。相反地,你应该有条不紊地去找出为什么出现了闪退。
最紧要的事是找出代码中导致闪退准确的位置:在哪个file和哪一行。X-code debugger可以帮助你,但是你应该学会如何高效地利用他,这些在这一节中会学到。
Getting Started
下载。如你所见,这是一个错误的程序!当你在X-code中打开这个程序,它至少有8个编译警告。这一节中,所用X-code版本为 4.3.(其实我用的是4.6.2).
Note: To follow along with this tutorial, the app needs to be run on the iOS 5 Simulator. If you run the app on your device, you’ll still get crashes, but they may not occur in the same order.
在模拟器中点击Run,出现如下结果:
hey,程序闪退了!
闪退主要有两种类型:SIGABRT (also called EXC_CRASH) andEXC_BAD_ACCESS (which can also show up under the names SIGBUS or SIGSEGV).
对于闪退来说,SIGABRT 是可控的闪退。因为系统意识到这个app在做一些不该做的事,所以app故意终止。
EXC_BAD_ACCESS 相对来说,debug就会比较困难,因为它只会在app完全毁坏的情况下,通常是因为memory management不当。
SIGABRT总是会在X-code Debug Output pane(下图)出现error message。
你能看到:
Problems[14465:f803] -[UINavigationController setList:]: unrecognized selector sent to
instance 0x6a33840
Problems[14465:f803] *** Terminating app due to uncaught exception 'NSInvalidArgumentException',
reason: '-[UINavigationController setList:]: unrecognized selector sent to instance 0x6a33840'
*** First throw call stack:
(0x13ba052 0x154bd0a 0x13bbced 0x1320f00 0x1320ce2 0x29ef 0xf9d6 0x108a6 0x1f743
0x201f8 0x13aa9 0x12a4fa9 0x138e1c5 0x12f3022 0x12f190a 0x12f0db4 0x12f0ccb 0x102a7
0x11a9b 0x2792 0x2705)
terminate called throwing an exception
在这些信息中包含了错误信息,
[UINavigationController setList:]: unrecognized selector sent to instance 0x6a33840
unrecognized selector sent to instance XXX 意味着app call a method that doesn‘t exist。在这里,setlist方法被UINavigationController错误地调用了,
The Exception Breakpoint
Fortunately, you can tell Xcode to pause the program at just that moment, using an Exception Breakpoint. A breakpoint is a debugging tool that pauses your program at a specific moment. You’ll see more of them in the second part of this tutorial, but for now you’ll use a specific breakpoint that will pause the program just before an exception gets thrown.我们可以使用Exception Breakpoint来使得程序停留在出现错误的代码位置。
可以用以下方法打开:
底部有个 小的+按钮,点击并选择 Add Exception Breakpoint。
添加以后,run the app。
现在能够获取到程序错误的位置咯。