ios - Dereferencing a __weak pointer is not allowed inside block -
apple docs can avoid strong reference cycle capturing weak reference self, this:
- (void)configureblock { xyzblockkeeper * __weak weakself = self; self.block = ^{ [weakself dosomething]; // capture weak reference // avoid reference cycle } }
yet when write code, compiler tells me:
dereferencing __weak pointer not allowed due possible null value caused race condition, assign strong variable first
yet doesn't following code create strong reference cycle, , possibly leak memory?
- (void)configureblock { xyzblockkeeper *strongself = self; self.block = ^{ [strongself dosomething]; } }
you should use one: eg:
__weak xyzblockkeeper *weakself = self; self.block = ^{ xyzblockkeeper *strongself = weakself; if (strongself) { [strongself dosomething]; } else { // bummer. <self> dealloc before run code. } }
Comments
Post a Comment