ARC actually preserves weak references before calling instance methods on them and frees them after the call.
I studied this problem and was fixed by a colleague after he showed him this stack question. He pointed it out: http://lists.apple.com/archives/objc-language/2012/Aug/msg00027.html
Of course, in the assembly, ARC saves and releases around the call via a weak link.
One time you want to listen to CLANG_WARN_OBJC_RECEIVER_WEAK to check for nil when nil can cause an error.
if (self.weakRefToParent) { //self.weakRefToParent could be dealloced and set to nil at this line NSString *name = [self.weakRefToParent name]; //CLANG_WARN_OBJC_RECEIVER_WEAK warning [self.namesArray addObject:name]; //name is nil, NSInvalidArgumentException }
This is a safer way:
Parent *strongRefToParent = self.weakRefToParent; if (strongRefToParent) { NSString *name = [strongRefToParent name]; [self.namesArray addObject:name]; }
James mitchell
source share