objective-c - gdb 远程返回错误 : E08 when calling completion block (EXC_BAD_ACCESS)

标签 objective-c automatic-ref-counting ios6 objective-c-blocks

我正在使用 ARC,在 iOS6 上我有时会遇到奇怪的崩溃: gdb 远程返回错误:E08

在堆栈跟踪中,之前的方法位于调用完成 block 的行上。我阅读了很多关于 block 和 ARC 的内容,但在以下上下文中使用它们时我仍然没有信心:

(简化方法并省略了一些代码)

- (void) method1: (void(^)(NSMutableArray *a)) completionBlock withFailedBlock:(void(^)(NSInteger errorCode,NSString *error)) failedBlock {
    __weak Controller *weakSelf = self;

    ...

    if(condition)
        completionBlock(weakSelf.a);

    //still do method2, since we might get updated data
    [weakself.service method2:^(NSMutableArray *a2) {
        weakSelf.shouldRefresh = NO;

        ...
        completionBlock(a2); //<-- sometimes crashes here
    } withFailedBlock:^(NSInteger errorCode, NSString *error) {
        failedBlock(errorCode, error);
    }];


} withFailedBlock:^(NSInteger errorCode, NSString *error) {
    failedBlock(errorCode, error);
}];

调用代码:

[[Controller sharedController] method1:^(NSMutableArray *a) {
    //save result in model (singleton)
    [Model sharedModel].a = a;
    [weakSelf refreshUI];

} withFailedBlock:^(NSInteger errorCode,NSString *error) {
    ;//show alert

}];

当我检查 block 及其周围的值时,它们看起来还不错。我也有 NSZombie。 我的 completionBlock 应该会被自动复制,因为它是从 block 中引用的。

我在这里错过了什么? 我也看到过 iOS5 和 4.3 上的崩溃,但从来没有 gdb remote 返回错误:E08。来自调试器的信息在这些情况下也没有帮助。我正在使用 PLWeakCompatibility这样我就可以在 iOS4.3 下支持 __weak

最佳答案

您是否还有这个问题?

为什么在这种情况下需要 weakSelf。在这种情况下,您似乎没有捕捉到 self 。当您省略它时,代码是否仍然会崩溃?

顺便说一句,当您尝试针对您的问题简化它时,我认为您的 method1 实现有点困惑。

- (void) method1: (void(^)(NSMutableArray *a)) completionBlock withFailedBlock:(void(^)(NSInteger errorCode,NSString *error)) failedBlock {
    ...
} withFailedBlock:^(NSInteger errorCode, NSString *error) {
    ...
}];

看起来不像是有效的方法实现。不应该是这样的:

- (void) method1: (void(^)(NSMutableArray *a)) completionBlock withFailedBlock:(void(^)(NSInteger errorCode,NSString *error)) failedBlock {
    ...
}

关于objective-c - gdb 远程返回错误 : E08 when calling completion block (EXC_BAD_ACCESS),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12809343/

相关文章:

ios - 如何定义全局结构?

objective-c - 从 subview 中更改 View

objective-c - "PerformSelector names a selector which retains the object"错误和 ARC

ios - 为什么当 ActiveReferenceCount > 0 时对象被取消初始化

ios - 通行证分组时,存折 passURL 未打开正确的通行证

iphone - iOS 6 中的 ALAssetsLibrary 和位置服务

ios - 具有可选 IBOutlet 的属性多态性

iphone - viewDidLoad 在 initWithFrame 之前被调用?

objective-c - 在其他对象内的容器中添加对象对于 arc 为 null

iOS 7 应用程序崩溃 EXC_CRASH (SIGABRT) objc_exception_throw 在 iOS 6 上工作正常