0

I have a BaseViewController which contains a property of type block:

typedef void (^result)(BOOL didSend);

@class ShakeView;

@interface BaseViewController : UIViewController

@property result mPlayerControlResultHandler;

@end

I want that this block can be accessible by other subclasses. So in viewDidLoad of BaseViewController I did this to initialise the block:

- (void)viewDidLoad
{
    [super viewDidLoad];

    _mPlayerControlResultHandler = ^(BOOL didSend)
    {
        if(!didSend)
        {
            __block BaseViewController *blocksafeSelf = self;

            [blocksafeSelf onOperatingSpeakerShortConnectionFailure];
        }
    };
}

This would throw a warning

Capturing self strongly in this block is likely to lead a retain cycle

So I searched for a solution this and tried everything suggested here but still unable to solve the problem.

Community
  • 1
  • 1
JLT
  • 3,052
  • 9
  • 39
  • 86

1 Answers1

2

Since the BaseViewController retains the block, and the block retains the objects it references, referring to self in the block causes a retain cycle.

The block doesn't modify the value of self, so it needn't be qualified with __block. Instead, it should be declared weak and moved outside of the block:

__weak BaseViewController *blocksafeSelf = self;

_mPlayerControlResultHandler = ^(BOOL didSend)
{
    if(!didSend)
    {
        [blocksafeSelf onOperatingSpeakerShortConnectionFailure];
    }
};
danh
  • 62,181
  • 10
  • 95
  • 136