NSSetUncaughtExceptionHandler无法捕获iPhone上的所有错误

mam*_*mcx 9 iphone crash error-handling

我正在使用http://code.google.com/p/google-toolbox-for-mac中的 GTMStackTrace .

我需要一种方法来测试最终用户在应用程序崩溃时向我发送错误.我知道如何将数据发送到我的网站,但问题是如何捕获所有未处理的错误.

我有这个代码:

void exceptionHandler(NSException *exception) {
    NSLog(@"%@", [exception reason]);
    NSLog(@"%@", [exception userInfo]);
    NSLog(@"%@", GTMStackTraceFromException(exception));

    UIAlertView *alert = [[UIAlertView alloc]
                          initWithTitle:NSLocalizedString(@"Error unexpected",@"Info: Can't save record")
                          message:GTMStackTraceFromException(exception) delegate:nil 
                          cancelButtonTitle:NSLocalizedString(@"Ok",@"Button: Ok") otherButtonTitles:nil];
    [alert show];
    [alert release];    
}

int main(int argc, char *argv[]) {
    //For crash report..
    NSSetUncaughtExceptionHandler(&exceptionHandler);
    //Normal code...
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    int retVal = UIApplicationMain(argc, argv, nil, nil);
    [pool release];
    return retVal;
}
Run Code Online (Sandbox Code Playgroud)

然而,事情不是抓了很多的错误,像一个坏的版本,一个BAD ACCES等,和App消失.我有2个问题,其中目前尚不清楚为什么happend和最终用户有没有什么要说的线索.

(例如,释放两次相同的var不能捕获)

那么,如何我得到的所有讨厌的错误,从而使最终用户简单的给我发一份崩溃报告吗?

Rob*_*ier 20

EXC_BAD_ACCESS不生成异常,它会生成一个信号(SIGSEGV).要抓住它,你需要一个信号处理程序.Christopher Atlan写了一个关于如何处理这两种崩溃的很好的解释.请务必阅读第1 部分第2部分.