Reputation: 948
I am implementing a Mac App and I want to handle following events:
If I detect them, I can send details to me to analyze and fix bugs using one of the Crash Handlers that I found. Alas I am unable to figure out how to intercept crashes and exceptions.
PS I tried following in my MyApp class
NSSetUncaughtExceptionHandler(&uncaughtExceptionHandler);
signal(SIGABRT, SignalHandler);
signal(SIGILL, SignalHandler);
signal(SIGSEGV, SignalHandler);
signal(SIGFPE, SignalHandler);
signal(SIGBUS, SignalHandler);
signal(SIGPIPE, SignalHandler);
but it doesn't work. Every time it crashes, it goes to debugger without classing SignalHandler or uncaughtExceptionHandler
Upvotes: 4
Views: 2322
Reputation: 122458
I have found the best way is to create a simple Exception handling delegate class as this allows exceptions in IBAction
methods to be caught.
main.mm:
@interface ExceptionDelegate : NSObject
@end
static ExceptionDelegate *exceptionDelegate = nil;
int main(int argc, char **argv)
{
int retval = 1;
@autoreleasepool
{
//
// Set exception handler delegate
//
exceptionDelegate = [[ExceptionDelegate alloc] init];
NSExceptionHandler *exceptionHandler = [NSExceptionHandler defaultExceptionHandler];
exceptionHandler.exceptionHandlingMask = NSLogAndHandleEveryExceptionMask;
exceptionHandler.delegate = exceptionDelegate;
//
// Set signal handler
//
int signals[] =
{
SIGQUIT, SIGILL, SIGTRAP, SIGABRT, SIGEMT, SIGFPE, SIGBUS, SIGSEGV,
SIGSYS, SIGPIPE, SIGALRM, SIGXCPU, SIGXFSZ
};
const unsigned numSignals = sizeof(signals) / sizeof(signals[0]);
struct sigaction sa;
sa.sa_sigaction = signalHandler;
sa.sa_flags = SA_SIGINFO;
sigemptyset(&sa.sa_mask);
for (unsigned i = 0; i < numSignals; i++)
sigaction(signals[i], &sa, NULL);
....
}
....
return retval;
}
static void signalHandler(int sig, siginfo_t *info, void *context)
{
logerr(@"Caught signal %d", sig);
exit(102);
}
@implementation ExceptionDelegate
- (BOOL)exceptionHandler:(NSExceptionHandler *)exceptionHandler
shouldLogException:(NSException *)exception
mask:(unsigned int)mask
{
logerr(@"An unhandled exception occurred: %@", [exception reason]);
return YES;
}
- (BOOL)exceptionHandler:(NSExceptionHandler *)exceptionHandler
shouldHandleException:(NSException *)exception
mask:(unsigned int)mask
{
exit(101);
// not reached
return NO;
}
@end
You'll need to add the ExceptionHandling.framework to your project.
Upvotes: 3