Xcode debug時如何看crash的call stack


debug的時候,會經常碰到crash之后xcode如下顯示:

*** First throw call stack:

(0x330f188f 0x35096259 0x3303a9db 0x96801 0x8ebbb 0xa03d9 0x9c2e3 0x49f63 0x66a8f 0x687ad 0x3258e86f 0x3258e7c5 0x3317c001 0x3541160d 0x330baf13 0x330c5523 0x330c54c5 0x330c4313 0x330474a5 0x3304736d 0x33194439 0x35626cd5 0x883a3 0x21f0)

terminate called throwing an exception(lldb) 

像是說:我生氣了,但是就不告訴你為什么.

網上找到一個好辦法:

在AppDelegate.m中添加函數:

void uncaughtExceptionHandler(NSException*exception){
    NSLog(@"CRASH: %@", exception);
    NSLog(@"Stack Trace: %@",[exception callStackSymbols]);
    // Internal error reporting
}

這里會打印出異常時的call stack, 也是發生crash時候, 最有價值的信息.

然后在程序入口處向下面這樣調用它:

-(BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions
{   
    NSSetUncaughtExceptionHandler(&uncaughtExceptionHandler);
    // Normal launch stuff
}
NSSetUncaughtExceptionHandler 就是告訴系統, 當發生異常時, 使用這個函數作為回調.
這樣之后, 發生crash時, 就可以看到打印出的call stack, 就可以很方便的定位到發生crash的那行代碼.




免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM