我發現最好的方法是創建一個簡單的異常處理委託類,因爲這允許IBAction
方法中的異常被捕獲。
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
你需要的ExceptionHandling.framework添加到您的項目。
謝謝。我補充說,爲了測試這個,你必須禁用GDB。如果啓用GDB,你的信號處理程序永遠不會被稱爲 – pierocampanelli
@pierocampanelli我不認爲這是真的,但是我使用LLDB。你使用的是什麼版本的Xcode? – trojanfoe
4.5.2我正在使用GDB。我需要禁用調試設置調試器爲無。 – pierocampanelli