我想使用块,但是几次调用后,它使我得到EXC_BAD_ACCESS。

我的代码:

- (void) sendBasket {
    if (currentSendToBasketBlock != nil) {
    // there's already a webservice going... set the new one as waiting
    waitingSendToBasketBlock = ^ {
        WebServicesModel *webServicesModel = [[[WebServicesModel alloc] init] autorelease];
        webServicesModel.delegate = self;
        [webServicesModel sendBasketToServer:currentBasket];

        [self showBasketBackground];

    };
    [waitingSendToBasketBlock copy];
} else {
    currentSendToBasketBlock = ^ {
        WebServicesModel *webServicesModel = [[[WebServicesModel alloc] init] autorelease];
        webServicesModel.delegate = self;
        [webServicesModel sendBasketToServer:currentBasket];

        [self showBasketBackground];
    };

    [currentSendToBasketBlock copy];

    currentSendToBasketBlock();
}
}

Web服务完成后,它将在同一对象上调用特定方法:
- (void) specificMethod {
    if (waitingSendToBasketBlock != nil) {
        waitingSendToBasketBlock(); // here, the EXC_BAD_ACCESS happens
        waitingSendToBasketBlock = nil;
    }
}

我想念什么?乐器找不到僵尸...

谢谢!

编辑:崩溃日志
Thread 0 name:  Dispatch queue: com.apple.libdispatch-manager
Thread 0:
0   libsystem_kernel.dylib          0x35590fbc kevent + 24
1   libdispatch.dylib               0x3525bed4 _dispatch_mgr_invoke + 744
2   libdispatch.dylib               0x3525cf3a _dispatch_queue_invoke + 70
3   libdispatch.dylib               0x3525c4ec _dispatch_worker_thread2 + 228
4   libsystem_c.dylib               0x3566758a _pthread_wqthread + 258
5   libsystem_c.dylib               0x35667bbc start_wqthread + 0

Thread 1 name:  WebThread
Thread 1:
0   libsystem_kernel.dylib          0x3558dc00 mach_msg_trap + 20
1   libsystem_kernel.dylib          0x3558d758 mach_msg + 44
2   CoreFoundation                  0x309582b8 __CFRunLoopServiceMachPort + 88
3   CoreFoundation                  0x3095a562 __CFRunLoopRun + 350
4   CoreFoundation                  0x308eaebc CFRunLoopRunSpecific + 224
5   CoreFoundation                  0x308eadc4 CFRunLoopRunInMode + 52
6   WebCore                         0x35f5327e _ZL12RunWebThreadPv + 382
7   libsystem_c.dylib               0x3566630a _pthread_start + 242
8   libsystem_c.dylib               0x35667bb4 thread_start + 0

Thread 2:
0   libsystem_kernel.dylib          0x35591cb0 stat + 12
1   CFNetwork                       0x34cccf56 DiskCookieStorage::syncStorageLocked() + 422
2   CFNetwork                       0x34c3fa60 PrivateHTTPCookieStorage::syncStorage() + 20
3   CFNetwork                       0x34ccaa7e HTTPCookieStorage::syncStorage() + 6
4   CFNetwork                       0x34ccaa9c HTTPCookieStorage::_syncTimerFired(__CFRunLoopTimer*, void*) + 12
5   CoreFoundation                  0x30957a40 __CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__ + 8
6   CoreFoundation                  0x30959ec4 __CFRunLoopDoTimer + 844
7   CoreFoundation                  0x3095a83e __CFRunLoopRun + 1082
8   CoreFoundation                  0x308eaebc CFRunLoopRunSpecific + 224
9   CoreFoundation                  0x308eadc4 CFRunLoopRunInMode + 52
10  Foundation                      0x341dd7f6 +[NSURLConnection(NSURLConnectionReallyInternal) _resourceLoadLoop:] + 206
11  Foundation                      0x341d0382 -[NSThread main] + 38
12  Foundation                      0x342425c6 __NSThread__main__ + 966
13  libsystem_c.dylib               0x3566630a _pthread_start + 242
14  libsystem_c.dylib               0x35667bb4 thread_start + 0

Thread 3 name:  com.apple.CFSocket.private
Thread 3:
0   libsystem_kernel.dylib          0x3558fc60 __select + 20
1   CoreFoundation                  0x3095d8f2 __CFSocketManager + 582
2   libsystem_c.dylib               0x3566630a _pthread_start + 242
3   libsystem_c.dylib               0x35667bb4 thread_start + 0

Thread 4:
0   libsystem_kernel.dylib          0x3558dc00 mach_msg_trap + 20
1   libsystem_kernel.dylib          0x3558d758 mach_msg + 44
2   CoreFoundation                  0x309582b8 __CFRunLoopServiceMachPort + 88
3   CoreFoundation                  0x3095a562 __CFRunLoopRun + 350
4   CoreFoundation                  0x308eaebc CFRunLoopRunSpecific + 224
5   CoreFoundation                  0x3092d6d2 CFRunLoopRun + 42
6   MyProject                       0x0011c41a +[ASIHTTPRequest runRequests] (ASIHTTPRequest.m:4773)
7   Foundation                      0x341d0382 -[NSThread main] + 38
8   Foundation                      0x342425c6 __NSThread__main__ + 966
9   libsystem_c.dylib               0x3566630a _pthread_start + 242
10  libsystem_c.dylib               0x35667bb4 thread_start + 0

Thread 5:
0   libsystem_kernel.dylib          0x355903ec __workq_kernreturn + 8
1   libsystem_c.dylib               0x356676d8 _pthread_wqthread + 592
2   libsystem_c.dylib               0x35667bbc start_wqthread + 0

Thread 6:
0   libsystem_kernel.dylib          0x355903ec __workq_kernreturn + 8
1   libsystem_c.dylib               0x356676d8 _pthread_wqthread + 592
2   libsystem_c.dylib               0x35667bbc start_wqthread + 0

Thread 7:
0   libsystem_kernel.dylib          0x355903ec __workq_kernreturn + 8
1   libsystem_c.dylib               0x356676d8 _pthread_wqthread + 592
2   libsystem_c.dylib               0x35667bbc start_wqthread + 0

最佳答案

您没有对副本执行任何操作,因此您仍在尝试访问存储在堆栈中的块。在调用copy的位置尝试此操作。

waitingSendToBasketBlock = Block_copy(waitingSendToBasketBlock);
//and
currentSendToBasketBlock = Block_copy(currentSendToBasketBlock);

关于objective-c - 使用块会导致EXC_BAD_ACCESS,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/6832458/

10-09 16:15