深入了解保留周期 [英] Understanding retain cycle in depth

查看:124
本文介绍了深入了解保留周期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们说我们有三个对象:祖父母,父母和孩子。祖父母保留父母,父母保留孩子,孩子保留父母。祖父母释放父母。

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 ?

推荐答案

除非有对父母或孩子的其他一些提及,他们都成了孤儿。但是父母和孩子之间的保留周期会阻止被释放并且它们会变成浪费的记忆。

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.

这篇关于深入了解保留周期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆