Guard PLCR initialization with dispatch_once instead of try/catch

PLCrashReporter may only be initialized once. So make sure the developer can't break this
This commit is contained in:
Andreas Linde 2013-08-17 15:36:20 +02:00
parent 22dd9006ab
commit 88627a48b4

View File

@ -528,7 +528,6 @@ NSString *const kBITCrashManagerStatus = @"BITCrashManagerStatus";
if (!_isSetup) { if (!_isSetup) {
BITPLCrashReporter *crashReporter = [BITPLCrashReporter sharedReporter]; BITPLCrashReporter *crashReporter = [BITPLCrashReporter sharedReporter];
NSError *error = NULL;
// Check if we previously crashed // Check if we previously crashed
if ([crashReporter hasPendingCrashReport]) { if ([crashReporter hasPendingCrashReport]) {
@ -536,9 +535,6 @@ NSString *const kBITCrashManagerStatus = @"BITCrashManagerStatus";
[self handleCrashReport]; [self handleCrashReport];
} }
// PLCrashReporter is throwing an NSException if it is being enabled again
// even though it already is enabled
@try {
// Multiple exception handlers can be set, but we can only query the top level error handler (uncaught exception handler). // Multiple exception handlers can be set, but we can only query the top level error handler (uncaught exception handler).
// //
// To check if PLCrashReporter's error handler is successfully added, we compare the top // To check if PLCrashReporter's error handler is successfully added, we compare the top
@ -553,9 +549,16 @@ NSString *const kBITCrashManagerStatus = @"BITCrashManagerStatus";
// get the current top level error handler // get the current top level error handler
NSUncaughtExceptionHandler *initialHandler = NSGetUncaughtExceptionHandler(); NSUncaughtExceptionHandler *initialHandler = NSGetUncaughtExceptionHandler();
// PLCrashReporter may only be initialized once. So make sure the developer
// can't break this
static dispatch_once_t plcrPredicate;
dispatch_once(&plcrPredicate, ^{
NSError *error = NULL;
// Enable the Crash Reporter // Enable the Crash Reporter
if (![crashReporter enableCrashReporterAndReturnError: &error]) if (![crashReporter enableCrashReporterAndReturnError: &error])
NSLog(@"[HockeySDK] WARNING: Could not enable crash reporter: %@", [error localizedDescription]); NSLog(@"[HockeySDK] WARNING: Could not enable crash reporter: %@", [error localizedDescription]);
});
// get the new current top level error handler, which should now be the one from PLCrashReporter // get the new current top level error handler, which should now be the one from PLCrashReporter
NSUncaughtExceptionHandler *currentHandler = NSGetUncaughtExceptionHandler(); NSUncaughtExceptionHandler *currentHandler = NSGetUncaughtExceptionHandler();
@ -569,10 +572,6 @@ NSString *const kBITCrashManagerStatus = @"BITCrashManagerStatus";
// this should never happen, theoretically only if NSSetUncaugtExceptionHandler() has some internal issues // this should never happen, theoretically only if NSSetUncaugtExceptionHandler() has some internal issues
NSLog(@"[HockeySDK] ERROR: Exception handler could not be set. Make sure there is no other exception handler set up!"); NSLog(@"[HockeySDK] ERROR: Exception handler could not be set. Make sure there is no other exception handler set up!");
} }
}
@catch (NSException * e) {
NSLog(@"[HockeySDK] WARNING: %@", [e reason]);
}
_isSetup = YES; _isSetup = YES;
} }