How can a weakly retained block cause a retain cycle when capturing “self”

I have a class with a property which is a weak reference to a block.

@interface BlockTest : NSObject
    @property (nonatomic, weak) void(^testBlock)();
@end

At another point in the class I use this block like this:

  • Copying blocks (ie: copying them to instance variables) in Objective-C
  • How to store blocks in properties in Objective-C?
  • Do I need to use a weak self pointer if a method called from a Block uses self?
  • ALAsset Photo Library Image Performance Improvements When Its Loading Slow
  • Is it possible to extend an existing Objective-C Block?
  • SWRevealViewController make sidebar button static & make the rearView appear on top of the frontView
  • - (void)foobar {
        self.testBlock = ^{
            [self doSomething];
        };
    }
    

    The compiler (Apple LLVM 3.0) complains that there might be a retain cycle because self is strongly captured here. But I fail to see how this leads to a retain cycle because the block itself is a __weakreference, so it should be fine. If I understood ARC weak references correctly, when the -foobar method returns the block passed to self.testBlock should be deallocated (if not held elsewhere) and thus also release self.

    What’s the reason the compiler still thinks there might be a retain cycle?

    Solutions Collect From Internet About “How can a weakly retained block cause a retain cycle when capturing “self””

    Blocks strongly capture objects within them regardless of how the block itself is referenced. The retain cycle warning is just that, a warning of the possibility. If you know based on the context of your app that this use will not cause a retain cycle you can safely ignore it. To get rid of the warning, you can pass self through an intermediary, strong or weak, as follows:

    __weak typeof(self) weakSelf = self;
    self.testBlock = ^{
        [weakSelf doSomething];
    };
    

    I’d change your block property to be a strong reference and do the above.