Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

straber

macrumors member
Original poster
Jul 3, 2012
60
0
I was recently playing with an app and it suddenly crashed (it's happened to all of us). But, when I re-launched the app I got an alert that said, "Our app quit unexpectedly, would you like to send an error report so we can fix this?" This made me curious for two reasons:

1.) How was their app able to detect that it had crashed?

2.) What information did their app send that would help them fix the crash?


Thank you for any help!
 
Last edited:

xArtx

macrumors 6502a
Mar 30, 2012
764
1
It might not be so sinister.
It could write a key when it exits and detect the
Value is incorrect at startup.

2) values of working variables, state, etc. all the usual.
 

Ides

macrumors member
Mar 27, 2012
95
0
I don't know much about this, but arent all app crashes recorded by the system and written to a log file somewhere? You can see this log file when you plug your device into Xcode.
 

straber

macrumors member
Original poster
Jul 3, 2012
60
0
I don't know much about this, but arent all app crashes recorded by the system and written to a log file somewhere? You can see this log file when you plug your device into Xcode.

It's true, you can get crash logs through Xcode, but that's not what I'm looking for. I'm trying to figure out how an app can detect that it's crashed all on its own after it's been released to customers.
 

xArtx

macrumors 6502a
Mar 30, 2012
764
1
Do this at startup:
Code:
	cleanexit = [[NSUserDefaults standardUserDefaults] integerForKey:@"exit"];
    if (cleanexit == 0) {// do something like send crash log}
 	cleanexit = 0;
    [[NSUserDefaults standardUserDefaults] setInteger:launchkeyval forKey:@"exit"];
    [[NSUserDefaults standardUserDefaults] synchronize];

and this on exit:

Code:
 	cleanexit = 1;
    [[NSUserDefaults standardUserDefaults] setInteger:launchkeyval forKey:@"exit"];
    [[NSUserDefaults standardUserDefaults] synchronize];

Now if the value read for cleanexit at startup is zero it is either the program's first launch,
or it crashed the last time it was run.
 

straber

macrumors member
Original poster
Jul 3, 2012
60
0
Do this at startup:
Code:
	cleanexit = [[NSUserDefaults standardUserDefaults] integerForKey:@"exit"];
    if (cleanexit == 0) {// do something like send crash log}
 	cleanexit = 0;
    [[NSUserDefaults standardUserDefaults] setInteger:launchkeyval forKey:@"exit"];
    [[NSUserDefaults standardUserDefaults] synchronize];

and this on exit:

Code:
 	cleanexit = 1;
    [[NSUserDefaults standardUserDefaults] setInteger:launchkeyval forKey:@"exit"];
    [[NSUserDefaults standardUserDefaults] synchronize];

Now if the value read for cleanexit at startup is zero it is either the program's first launch,
or it crashed the last time it was run.

This seems like the best way to do it to me. Thanks! Now my only question is, when my app becomes active, what information does it have access to about the previous crash that I can have it send me?
 

ArtOfWarfare

macrumors G3
Nov 26, 2007
9,560
6,059
This seems like the best way to do it to me. Thanks! Now my only question is, when my app becomes active, what information does it have access to about the previous crash that I can have it send me?

You could keep track of when large tasks are in progress... maybe if it takes 5 seconds to parse a large XML file, for example, you could know which file it was parsing when it crashed.
 

straber

macrumors member
Original poster
Jul 3, 2012
60
0
I ended up writing this function in my app delegate.

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

and added this line of code inside appDidFinishLaunching.

Code:
NSSetUncaughtExceptionHandler(&uncaughtExceptionHandler);

This will call the above function anytime an exception is not handled, and you have access to the exception. From that function you can log the exception however you like, store it to a key in the defaults seems easiest, then check if there is any data for that key when the app launches.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.