It is known that a block may induce a retain cycle if the block implicitly retains an object that also retains the block. Example:
self.block = ^{
[self foo];
};
A commonly prescribed workaround is to simply use a __weak self to avoid the retain cycle:
__typeof(self) __weak weakSelf = self;
self.block = ^{
[weakSelf foo];
};
However, this can cause problems if self is dealloced in the middle of executing foo. A better strategy is to capture a __strong reference to the __weak self locally within the block. However, I don't know whether the __weak modifier is captured __typeof, and if it is, can I override it with another __strong?
So, one of these, is correct, but which?
A
__typeof(self) __weak weakSelf = self;
self.block = ^{
__typeof(self) strongWeakSelf = weakSelf;
[strongWeakSelf foo];
};
B
__typeof(self) __weak weakSelf = self;
self.block = ^{
__typeof(self) __strong strongWeakSelf = weakSelf;
[strongWeakSelf foo];
};
C
__typeof(self) __weak weakSelf = self;
self.block = ^{
__typeof(weakSelf) strongWeakSelf = weakSelf;
[strongWeakSelf foo];
};
D
__typeof(self) __weak weakSelf = self;
self.block = ^{
__typeof(weakSelf) __strong strongWeakSelf = weakSelf;
[strongWeakSelf foo];
};
I hope that C is correct because it doesn't place self inside the block at all, and doesn't require an additional __strong.
EDIT
I totally screwed up my initial question and had my examples wrong. Inside my blocks, all my answers used to be [weakSelf foo] instead of [strongWeakSelf foo].