How to fix “Capturing 'block' strongly in this block is likely to lead to a retain cycle”

不羁的心 提交于 2019-11-28 18:26:45

WARNING: Block pointer variable 'completionBlock' is uninitialized when captured by the block

This happens because block variables initialized to a recursive block need __block storage.

  • Variables within a block are copied unless declared with __block, in which case they are passed as reference.
  • When a recursive block is assigned to a block variable, the creation happens before the assignment, and such creation triggers a variable copy. Given that the variable hasn't been assigned yet, the copied variable will be a bad value, and it will produce a crash when the block is ran.
  • But if we add __block, the block will be created with a reference to the variable instead. Then the variable will be initialized to the created block, and the block will be ready to use.

WARNING: Capturing 'completionBlock' strongly in this block is likely to lead to a retain cycle

This happens because a block variable is a strong reference to the block, and the block is itself referencing the variable (because as we saw before, the variable has a __block so it is referenced instead copied).

So we need

  • A weak reference to the strong variable inside the block.
  • And a strong reference outside to prevent the block from being deallocated during the scope of the method where it is created.
    void(^ completionBlock) (id obj, NSError *err, NSURLRequest *request);
    void(^ __block __weak weakCompletionBlock) (id obj, NSError *err, NSURLRequest *request);
    weakCompletionBlock = completionBlock = ^(id obj,NSError *err, NSURLRequest *request){
        [self lengthyAsyncMethod:weakCompletionBlock];
    };

The name doLengthyAsynchronousOperationWithCompletionBlock suggests that the method may outlive the method scope where the block is created. Given that the compiler doesn't copy a block passed as an argument, it's responsibility of this method to copy this block. If we are using this block with block aware code (eg: dispatch_async()), this happens automatically.

Had we been assigning this block to an instance variable, we would need a @property(copy) and a weak reference to self inside the block, but this is not the case, so we just use self.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!