Lets say we have three objects: a grandparent, parent and child. The grandparent retains the parent, the parent retains the child and the child retains the parent. The grandparent releases the parent.
What will happen in this case ?
Lets say we have three objects: a grandparent, parent and child. The grandparent retains the parent, the parent retains the child and the child retains the parent. The grandparent releases the parent.
What will happen in this case ?
Since the P object has retainCount of 1, when it is released, its retainCount goes to 0, and its dealloc method is called; This in turn calls release on C object, whose retain count also goes to 0; and its dealloc method is called.
Both objects P and C will get freed.
When C object's dealloc method is called, in turn GP object's release is called, but since GP holds a retain count of 2, the retain count is decremented to 1, and it continues to hang around.
Retain cycle is a deadlock condition. Real Life Example of Retain Cycle: If two object hold a reference each other and no other object is released.
Example: Rummy Game
In a simple case, consider two objects A and B where A creates and retains B. When A is created, it creates B. When whoever created A finally releases it, A's retain count drops to zero and it gets deallocated. If A's dealloc method calls release on B, B's retain count also drops to zero and it also gets deallocated. [This assumes that nobody else has retained A or B, because I'm keeping things simple.]
But what happens if B needs a reference back to A, and it retains A? Whoever created A might release it. But since B has also retained A, A's retain count won't go to zero. Likewise, since A retains B, B's retain count also won't go to zero. Neither will be deallocated. Even if B calls A's release method in its own dealloc it doesn't matter, because that method is never going to be called.
At this point you have a memory leak, because you don't have any reference to A or B even though they both still exist. If A or B is doing anything processor intensive then you might also be leaking CPU time to unwanted objects.
In your case A is parent and B is child and whosoever created A is grandparent.
A retain cycle is a loop that happens when Object A retains Object B, and Object B retains Object A. In that situation, if either object is released:
Thus, those two objects will just hang around in memory for the life of the program even though they should, if everything were working properly, be deallocated.
Grandparent: John Parent: Ted Child: Mary
Here is my example using a telephone call for illustration:
John calls Ted and wants to do a conference call with Mary.
Ted Says to John: "Hang on the line, and I will dial in Mary"
Ted leaves John on hold and calls Mary who promptly answers the phone.
Mary says to Ted: "Merge my call in with John and I WILL NOT hang up until I'm through"
Ted, having not heard back from John in a while, leaves the call to do something else.
John goes to merge the calls with Ted and Mary and then suddenly dies.
Mary is stuck on the line to John but will never hang up cause John ain't coming back!
Unless there is some other reference to the parent or child, they both become orphaned. But the retain cycle between the parent and child prevent either from being released and they become wasted memory.
A child should never retain a parent. If anything, use a weak reference in the child to maintain a reference to the parent.