Retain cycle


By running the example that test the strong and weak references between the two BNRItem - backpack and calculator, the console was clearly stating that backpack was destroyed because of setting nil to it, that it also showed the container of calculator set null automatically. So was the calculator destroyed. The code snippet as follows, copied from the book.

backpack = nil; NSLog(@"Container: %@", [calculator container]); calculator = nil;

I tried something on top of it first by comment the third row, but the calculator was still destroyed. I’m not sure why it happened.
On contrary, I just kept ‘calculator = nil;’ at the beginning, and comment the ‘backpack = nil;’, both backpack and calculator were destroyed.
What is the mechanism behind?

And what would happen to the two instance variables container and containedItem. I just got to know container is set nil when ‘parent’ (backpack) is destroyed, but how about containedItem? And also what if ‘child’ (calculator) is destroyed.

Thank you so much for someone’s help!!


See my response here: