Suppose I have an object with a strong reference to a block. Sometime during the execution of that block, the strong reference is set to nil. Is the block guaranteed to finish its execution, or can this cause a crash? I've seen exc-bad-access errors, but I can't produce them reliably, so I don't know exactly why they pop up.
For example:
-(void)method
{
self.block = ^{
//code
self.block = nil;
//more code - crash here?
}
}
-(void)otherMethod
{
block();
}
The documents don't seem to guarantee that a block will be retained while it is executing. Conversely, the documentation for GCD calls such as dispatch_async
does make such guarantees. From that it would seem you can't assume that an ordinary call into a block will retain it.
So in your code you probably want:
-(void)otherMethod
{
dispatch_block_t localBlock = Block_copy(block);
localBlock();
Block_release(localBlock);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With