【问题标题】:Catching exceptions generated within dispatch_once blocks捕获 dispatch_once 块中生成的异常
【发布时间】:2017-11-24 13:14:49
【问题描述】:

在处理 Apple 的按需资源框架时,我的应用发现为一部分用户生成了许多异常。我解决此问题的第一次尝试是将我的代码包装在 @try/@catch 块中,但不幸的是,崩溃仍然传播。我已经缩小了问题的范围,但不确定如何解决。似乎 Apple 使用 NSBundleResourceRequest 的代码使用了一个 dispatch_once 块,其中会生成一个 Obj-C 异常。我编写了以下测试代码来查看使用 dispatch_once 块处理异常的行为:

 static dispatch_once_t onceToken;
 @try {

    // 1.) Expected: Caught by first catch block
    // @throw [NSException exceptionWithName:NSInternalInconsistencyException reason:@"Couldn't find" userInfo:nil];

    // 2.) Expected: Caught by second catch block
    // @throw NSInternalInconsistencyException;

    // 3.) Will not be caught be the surrounding @try/@catch block
    dispatch_once(&onceToken, ^{
        @throw [NSException exceptionWithName:NSInternalInconsistencyException reason:@"Couldn't find" userInfo:nil];
    });
} @catch(NSException* e) {
    DDLogError(@"Catch: NSException");
} @catch(id e) {
    DDLogError(@"Catch: Obj-C");
} @catch (...) {
    DDLogError(@"Catch: ...");
}

从我的 cmets 中可以看出,在 dispatch_once 块中生成的异常不会被周围的 @try/@catch 捕获。

我的问题是:

如何捕获在 dispatch_once 块中生成的异常,我无法访问其源代码(Apple 的)?

关于这个问题,除了不使用 Apple 的 ODR 框架之外,我还能做些什么来让我的应用更加稳定?

附加信息(如果有帮助):

我要解决的具体堆栈跟踪是以下代码。这不是 NSBundleResourceRequest 的唯一问题,所有其他问题都遵循相同的范例。

Application Specific Information:
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'NSBundleResourceRequest could not connect to its helper daemon'

Last Exception Backtrace:
0   CoreFoundation                       0x00000001857f3d38 __exceptionPreprocess + 124
1   libobjc.A.dylib                      0x0000000184d08528 objc_exception_throw + 52
2   Foundation                           0x000000018623e9d0 _setupConnection + 1688
3   libdispatch.dylib                    0x0000000185179048 _dispatch_client_callout + 12
4   libdispatch.dylib                    0x000000018517c710 dispatch_once_f$VARIANT$mp + 56
5   Foundation                           0x000000018623e2e8 +[NSBundleResourceRequest _connection] + 64
6   Foundation                           0x000000018623d1ac -[_NSBundleODRDataForApplications initWithBundle:] + 124
7   Foundation                           0x000000018623d07c +[_NSBundleODRDataForApplications dataForBundle:createIfRequired:] + 180
8   Foundation                           0x000000018623fba0 -[NSBundleResourceRequest conditionallyBeginAccessingResourcesWithCompletionHandler:] + 76

【问题讨论】:

  • 嗨,你有答案了吗?
  • 不,我没有。我不确定这是否可能。

标签: ios exception-handling


【解决方案1】:

因为块在 GCD 调用中运行

void _dispatch_client_callout(void *ctxt, dispatch_function_t f)
{
    @try {
        return f(ctxt);
    }
    @catch (...) {
        objc_terminate();
    }
}

捕获 NSException。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-01-27
    • 1970-01-01
    • 2010-09-28
    • 2012-11-11
    • 2011-04-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多