自定义NSUncaughtExceptionHandler调用上一个异常处理程序

问题描述 投票:3回答:2

我的应用程序包括崩溃报告库,使用NSSetUncaughtExceptionHandler来捕获崩溃。我需要在崩溃报告实现之后的\之前实现自定义操作(日志崩溃和显示警报视图)。要实现此行为,首先我使用NSGetUncaughtExceptionHandler()保留对先前UncaughtExceptionHandler的引用,然后注册我的自定义异常处理程序和信号处理程序。在我的自定义处理程序中,我尝试在自定义操作之后执行前一个处理程序,但是这会为previousHandler(异常)抛出信号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没有调用的任何想法?
ios objective-c exception uncaughtexceptionhandler
2个回答
3
投票

不要注册信号处理程序。我必须对下面提供的代码进行模糊处理,但它来自App Store上的生产应用程序:

AppDelegate应用程序:didFinishLaunchingWithOptions:

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

转会:

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

    if (fabricHandler) {
        fabricHandler(exception);
    }
}

0
投票

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

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

HTH

© www.soinside.com 2019 - 2024. All rights reserved.