4

我的应用程序包含NSSetUncaughtExceptionHandler用于捕获崩溃的崩溃报告库。我需要在崩溃报告实现之前\之后实现自定义操作(记录崩溃并显示警报视图)。为了实现这种行为,首先我使用 来保留对先前 UncaughtExceptionHandler 的引用NSGetUncaughtExceptionHandler(),然后注册我的自定义异常处理程序和信号处理程序。在我的自定义处理程序中,我尝试在自定义操作之前\之后执行上一个处理程序,但这会为 previousHandler(exception) 抛出一个信号 SIGABRT(在这两种情况下)。这是代码示例:

static NSUncaughtExceptionHandler *previousHandler;

void InstallUncaughtExceptionHandler()
{
    // keep reference to previous handler
    previousHandler = NSGetUncaughtExceptionHandler();

    // register my custom exception handler
    NSSetUncaughtExceptionHandler(&HandleException);
    signal(SIGABRT, SignalHandler);
    signal(SIGILL, SignalHandler);
    signal(SIGSEGV, SignalHandler);
    signal(SIGFPE, SignalHandler);
    signal(SIGBUS, SignalHandler);
    signal(SIGPIPE, SignalHandler);
}

void HandleException(NSException *exception)
{
    // execute previous handler 
    previousHandler(exception);
    // my custom actions
}

void SignalHandler(int signal)
{
    NSLog(@"SignalHandler");
}
  1. 如何在不抛出信号的情况下执行先前的处理程序?
  2. 任何想法为什么SignalHandler在系统抛出信号时不调用?
4

2 回答 2

3

不要注册信号处理程序。我必须对下面显示的代码进行一些混淆,但它来自 App Store 上的生产应用程序:

AppDelegate 应用程序:didFinishLaunchingWithOptions:

fabricHandler = NSGetUncaughtExceptionHandler();
NSSetUncaughtExceptionHandler(&customUncaughtExceptionHandler);

处理程序:

void customUncaughtExceptionHandler(NSException *exception) {
    // Custom handling

    if (fabricHandler) {
        fabricHandler(exception);
    }
}
于 2015-11-02T08:46:27.180 回答
0

PreviousSignalHandler 可能 1) 重置所有设置的信号处理程序 2) 调用中止

它将中止的原因之一。所以你可以做所有你想做的事情并调用前一个处理程序。

高温高压

于 2017-12-22T09:34:34.963 回答