jssblck
jssblck

Reputation: 529

Strong reference to self inside a method running from a block

So, I know that if you save a block inside of self, then access self inside of that block you need to create and use something like __weak id weakSelf = self;.

My question is, does this also extend to functions being called from that block? As in, would the following lead to a retain cycle:

self.block = ^{ [weakSelf myFunction]; }
- (void) myFunction { self.counter++; }

Thanks for your time!

Upvotes: 1

Views: 126

Answers (1)

Léo Natan
Léo Natan

Reputation: 57060

This does not create a retain cycle, because the self in the method is actually a parameter passed by the Objective C runtime (using objc_msgSend and similar). So let's consider the two scenarios possible in your code example. weakSelf has been zeroed out due to release of the holding object - a message passed to nil is ignored. weakSelf is not zeroed, in which case, it is passed by the Objective C runtime to the message as its self parameter.

Upvotes: 2

Related Questions