问题
I'd like to use blocks, but it causes me a EXC_BAD_ACCESS after a few calls.
My code:
- (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();
}
}
When the webservice is finished, it calls a specific method on the same object:
- (void) specificMethod {
if (waitingSendToBasketBlock != nil) {
waitingSendToBasketBlock(); // here, the EXC_BAD_ACCESS happens
waitingSendToBasketBlock = nil;
}
}
What am I missing? Instruments doesn't find a Zombie...
Thanks!
Edit: Crash Log
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
回答1:
You are not doing anything with your copy, therefore you are still attempting to access the block that was stored on the stack. Try this in the place of your call to copy
.
waitingSendToBasketBlock = Block_copy(waitingSendToBasketBlock);
//and
currentSendToBasketBlock = Block_copy(currentSendToBasketBlock);
来源:https://stackoverflow.com/questions/6832458/using-blocks-causes-exc-bad-access