NSSetUncaughtExceptionHandler not catch all errors

2019-01-13 07:09发布

问题:

I'm using GTMStackTrace from http://code.google.com/p/google-toolbox-for-mac.

I need a way to test end-user to send me errors when the app crash. I know how send data to my website but the problem is how catch all non-handled errors.

I have this code:

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;
}

However, the thing not caught a lot of errors, like a bad release, a BAD ACCES, etc, and the App disappear. I have 2 issues where is not clear why happend and the end-users have not clue about what to say.

(For example release twice the same var is not catch)

So, how I get ALL that pesky errors so the end-user simple send me a crash report?

回答1:

EXC_BAD_ACCESS doesn't generate an exception, it generates a signal (SIGSEGV). To catch it, you need a signal handler. Christopher Atlan wrote a nice explanation of how to handle both kinds of crashes. Be sure to read both part 1 and part 2.



回答2:

if anyone is still dealing with this problem and can't work it out even with SIGSEGV see my post ... EXC_BAD_ACCESS automatic handling