I believe that I finally have a satisfactory answer to this question. Note that this is all in the context of ARC.
A block may be freed during its execution. The block will continue to execute as usual, but any of its pointers to captured variables becomes suspicious (and potentially dangerous).
Suppose ObjectA has a copy-copy property called replenishment:
@property (nonatomic, copy) void (^completion)();
... where the assignment looks something like this:
__weak ObjectA * weakSelf = self; self.completion = ^{ weakSelf.completion = nil; [weakSelf doSomethingElse]; };
If the block is called like that ...
-(void)method { _completion();
... then, assuming nothing else refers to this instance of the block, it is freed and its captured variable weakSelf becomes zero. doSomethingElse is never called. The best way around this is to simply call the block using its accessor - this will allocate a new copy on the stack. The original will be released, but the new copy and all its captured variables will be saved in the current context.
-(void)method { self.completion();
Aaron source share