Understanding retain cycle in depth

2019-03-08 01:43发布

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 ?

9条回答
▲ chillily
2楼-- · 2019-03-08 01:58

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.

查看更多
等我变得足够好
3楼-- · 2019-03-08 02:01

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

查看更多
够拽才男人
4楼-- · 2019-03-08 02:03

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.

查看更多
姐就是有狂的资本
5楼-- · 2019-03-08 02:04

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:

  • Object A won't be deallocated because Object B holds a reference to it (retain count > 0).
  • Object B won't ever be deallocated as long as Object A has a reference to it (retain count > 0).
  • But Object A will never be deallocated because Object B holds a reference to it (retain count > 0).
  • till infinity

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.

查看更多
唯我独甜
6楼-- · 2019-03-08 02:11

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!

查看更多
时光不老,我们不散
7楼-- · 2019-03-08 02:12

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.

查看更多
登录 后发表回答