IOS開發-如何debug及處理閃退,My App Crashed,Now What? - P...

我只寫些相對來講,比較有用的信息,想完整閱讀的,能夠自行去原文。。 app

原文來自:http://www.raywenderlich.com/10209/my-app-crashed-now-what-part-1 學習



學習如何Debug和處理App閃退 this

處理閃退不必定很難,除非你精神賁潰而且胡亂地更改代碼,指望這個bug會奇蹟般的消失。。相反地,你應該有條不紊地去找出爲何出現了閃退。 spa


最緊要的事是找出代碼中致使閃退準確的位置:在哪一個file和哪一行。X-code debugger能夠幫助你,可是你應該學會如何高效地利用他,這些在這一節中會學到。 debug


Getting Started code

下載example project。如你所見,這是一個錯誤的程序!當你在X-code中打開這個程序,它至少有8個編譯警告。這一節中,所用X-code版本爲 4.3.(其實我用的是4.6.2). cdn

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. ip

在模擬器中點擊Run,出現以下結果: ci

hey,程序閃退了! get


閃退主要有兩種類型: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。

如今可以獲取到程序錯誤的位置咯。

相關文章
相關標籤/搜索