Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Crashlytics and custom NSUncaughtExceptionHandler

I need to implement crash reporting in a static library I ship out to other apps. For this, I am registering myself as the NSUncaughtExceptionHandler:

NSSetUncaughtExceptionHandler(HsWatchdogUncaughtExceptionHandler);
signal(SIGABRT, SignalHandler);
signal(SIGILL, SignalHandler);
signal(SIGSEGV, SignalHandler);
signal(SIGFPE, SignalHandler);
signal(SIGBUS, SignalHandler);
signal(SIGPIPE, SignalHandler);

I also keep a reference to the previous UncaughtExceptionHandler before this using NSGetUncaughtExceptionHandler()

Since a lot of apps using my library also have their own crash reporting mechanisms (usually Crashlytics), I need my lib to play along nicely.

What I plan to do is, after my exceptionHandler is called with the NSException, I would like to pass it on to the 'previous UncaughtExceptionHandler'.

Will this work:

self.previousUncaughtExceptionHandler(exception);
like image 510
NSRover Avatar asked Sep 01 '15 11:09

NSRover


2 Answers

Declare a variable to store the previous handler

static NSUncaughtExceptionHandler *_previousHandler;

First get the previous handler and store it in a global:

_previousHandler = NSGetUncaughtExceptionHandler();

Then create your own handler :

void onException(NSException * exception) {
    // do what you want to do ... then call the previous handler
    _previousHandler(exception);
}

Set it as the uncaught exception handler :

 NSSetUncaughtExceptionHandler(&onException)
like image 140
Samhan Salahuddin Avatar answered Nov 15 '22 05:11

Samhan Salahuddin


Saw this and it's a few years old, but I'm not sure implementing your own exception handler is a supported or will work. It really depends how the 3rd party libraries implemented their support. But two of the larger 3rd party libraries (HockeyApp and Crashlytics) advise against implementing your own exception handler in conjunction with their library. https://support.hockeyapp.net/discussions/problems/34639-exception-reason-text-is-missing-in-crash-reports

https://support.hockeyapp.net/discussions/problems/11409-hockeyapp-does-not-generate-crash-report "2. There is a uncaughtExceptionHandler implemented that catches the exception, please verify such a method does not exist in your code."

Is it safe to use NSSetUncaughtExceptionHandler with Fabric? “Having two UncaughtExceptionHandlers on iOS isn't supported and is not recommended.” “You should only use one uncaught exception handler on iOS, I personally don't recommend the approach you're discussing”

The Realm team discusses how the handle using NSSetUncaughtExceptionHandler in conjunction with a 3rd party library so if you decide to do this you might take a look at how they implemented it. https://github.com/realm/realm-cocoa/issues/3822

like image 21
xdeleon Avatar answered Nov 15 '22 04:11

xdeleon