Apple docs say I can avoid a strong reference cycle by capturing a weak reference to self, like this:
- (void)configureBlock {
XYZBlockKeeper * __weak weakSelf = self;
self.block = ^{
[weakSelf doSomething]; // capture the weak reference
// to avoid the reference cycle
}
}
Yet when I write this code, the compiler tells me:
Dereferencing a __weak pointer is not allowed due to possible null value caused by race condition, assign it to strong variable first
Yet doesn't the following code create a strong reference cycle, and possibly leak memory?
- (void)configureBlock {
XYZBlockKeeper *strongSelf = self;
self.block = ^{
[strongSelf doSomething];
}
}