【问题标题】:Custom NSUncaughtExceptionHandler call previous exception handler自定义 NSuncaughtExceptionHandler 调用之前的异常处理程序
【发布时间】:2016-02-02 01:36:44
【问题描述】:

我的应用包含崩溃报告库,它使用NSSetUncaughtExceptionHandler 来捕获崩溃。 我需要在崩溃报告实施之前\之后实施自定义操作(记录崩溃并显示警报视图)。 为了实现这种行为,首先我使用NSGetUncaughtExceptionHandler() 保留对先前UncaughtExceptionHandler 的引用,然后注册我的自定义异常处理程序和信号处理程序。在我的自定义处理程序中,我尝试在自定义操作之前\之后执行上一个处理程序,但这会为 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 在系统抛出信号时不调用吗?

【问题讨论】:

    标签: ios objective-c exception uncaughtexceptionhandler


    【解决方案1】:

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

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

    HTH

    【讨论】:

      【解决方案2】:

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

      AppDelegate 应用程序:didFinishLaunchingWithOptions:

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

      处理程序:

      void customUncaughtExceptionHandler(NSException *exception) {
          // Custom handling
      
          if (fabricHandler) {
              fabricHandler(exception);
          }
      }
      

      【讨论】:

      • 我还添加了 'if' 语句,但它仍然崩溃。您认为信号处理程序会导致问题吗?
      • 尝试不使用它们。它们不是必需的,因为默认处理程序调用 NSGetUncaughtExceptionHandler() 返回的任何内容。您可能正在创建某种循环,但我不确定。
      猜你喜欢
      • 1970-01-01
      • 2011-07-13
      • 2015-10-18
      • 2023-03-04
      • 1970-01-01
      • 2015-12-05
      • 1970-01-01
      • 1970-01-01
      • 2020-10-10
      相关资源
      最近更新 更多