aprofromindia
aprofromindia

Reputation: 1129

iOS ARC block retain cycle

Can someone confirm if the block below is turning into a retain cycle please? Please note the block is being called by SampleClass2 not SampleClass1.

@interface SampleClass1{
    NSArray *_array;
}

@implementation SampleClass1

-(void) doSomething {
    SampleClass2 *sampleClass2 = [[SampleClass2 alloc] init];
    [sampleClass2 doAnother:^(NSArray *anotherArray){
        _array = anotherArray;      // _array is an ivar
    }];
}

@end

Upvotes: 0

Views: 945

Answers (2)

newacct
newacct

Reputation: 122509

  • Does the block retain self? Yes.
  • Does sampleClass2 retain the block? Maybe. It depends on what the doAnother: method does. Without the code, it's impossible to say.
  • Even if we assume that sampleClass2 retains the block, is there a retain cycle? No. There is a connection sampleClass2 -> the block -> self, but nowhere from the code shown is there a connection from self to sampleClass2.

Upvotes: 1

Nikolai Ruhe
Nikolai Ruhe

Reputation: 81878

There only can be a retain cycle when the block is kept around in an ivar or property. We don't see what -[SampleClass2 doAnother:] does with the block, so we don't know.

The block does capture self implicitly by referencing the ivar _array, so there's a chance that a reference cycle is formed. It depends on who retains the SampleClass1 instance and what SampleClass2 does with the block.

Upvotes: 0

Related Questions