6

我有一个自定义的 NSOperation 子类,用于发出 HTTP 请求。它接受在 NSOperation 完成时执行的基于块的回调。一切都相应地工作,但我在尝试执行我的完成回调时遇到了奇怪的间歇性崩溃。我读过很多基于块的 EXEC_BAD_ACCESS 问题是由于在将块传递给其他方法时未正确复制块引起的。

我相信我的问题与我如何使用块有关。我将在下面包含我的应用程序的标准用例。我的问题的根源可能在于涉及区块的所有权误解。

// Perform a HTTP request to a specified endpoint and declare a callback block
[self performRequestToEndpoint:@"endpoint" completion:^(HTTPResponse *response) {
    NSLog(@"Completed with response: %@", response);
}];

// A helper function to avoid having to pass around too many parameters
- (void)performRequestWithEndpoint:(NSString *)endpoint completion:(void (^)(HTTPResponse *response))completionBlock
{
    // Make our HTTP request and callback our original completion block when done
    [self requestWithMethod:@"GET" path:endpoint completion:^(HTTPResponse *response) {
        if(![response error])
        {
            // Call our original completion block
            completionBlock(response);
        }
    ];
}

当通过 requestWithMethod:path:completion: 方法分配回调块时,它被复制如下:

@property (nonatomic, copy) void (^operationCompletionBlock)(HTTPResponse *response);

这是崩溃的重点:

- (void)callCompletionBlockWithResponse:(id)response
{
    if(self.operationCompletionBlock && !self.isCancelled)
    {
        self.operationCompletionBlock(response); // crashes here (intermittently)
    }

    [self finish];
}

下面附上堆栈跟踪:

* thread #1: tid = 0x2403, 0x0000000000000000, stop reason = EXC_BAD_ACCESS (code=1, address=0x0)
    frame #0: 0x0000000000000000
    frame #1: 0x00007f946b53ed01
    frame #2: 0x0000000102da7cf7 Project`-[HTTPRequest callCompletionBlockWithResponse:] + 215 at HTTPRequest.m:402
    frame #3: 0x0000000102da79e7 Project`__44-[HTTPRequest connectionDidFinishLoading:]_block_invoke_0 + 423 at HTTPRequest.m:381
    frame #4: 0x00007fff956fea86 libdispatch.dylib`_dispatch_call_block_and_release + 18
    frame #5: 0x00007fff957008f6 libdispatch.dylib`_dispatch_main_queue_callback_4CF + 308
    frame #6: 0x00007fff8f07ce7c CoreFoundation`__CFRunLoopRun + 1724
    frame #7: 0x00007fff8f07c486 CoreFoundation`CFRunLoopRunSpecific + 230
    frame #8: 0x00007fff94f1a4d3 HIToolbox`RunCurrentEventLoopInMode + 277
    frame #9: 0x00007fff94f21781 HIToolbox`ReceiveNextEventCommon + 355
    frame #10: 0x00007fff94f2160e HIToolbox`BlockUntilNextEventMatchingListInMode + 62
    frame #11: 0x00000001032a6e31 AppKit`_DPSNextEvent + 659
    frame #12: 0x00000001032a6735 AppKit`-[NSApplication nextEventMatchingMask:untilDate:inMode:dequeue:] + 135
    frame #13: 0x00000001032a3071 AppKit`-[NSApplication run] + 470
    frame #14: 0x000000010351f244 AppKit`NSApplicationMain + 867
    frame #15: 0x0000000102d69512 Project`main + 34 at main.m:13
    frame #16: 0x0000000102d694e4 Project`start + 52
4

1 回答 1

3

This is a shot in the dark. You have two completion blocks, only one of which you explicitly copy (using the property). My mental model says that the completionBlock passed to performRequestWithEndpoint:completion: should be captured in the scope of the block you pass on. But I know some paranoid people who might try this:

- (void)performRequestWithEndpoint:(NSString *)endpoint 
                        completion:(void (^)(HTTPResponse *response))completionBlock
{
    void (^copiedBlock)(HTTPResponse *response) = [completionBlock copy];

    [self requestWithMethod:@"GET" path:endpoint completion:^(HTTPResponse *response) {
        if(![response error] && copiedBlock) {
            copiedBlock(response);
        }
    ];
}    
于 2014-02-10T21:23:40.957 回答