gpt4 book ai didi

objective-c - 什么是 "parent"和 "child"对象?

转载 作者:行者123 更新时间:2023-12-02 15:33:48 25 4
gpt4 key购买 nike

我看到一个关于引用计数的问题:

以三个对象为例:祖 parent 、 parent 和 child 。祖 parent 留住 parent , parent 留住 child , child 留住 parent 。祖 parent 释放 parent 。解释发生了什么

“祖 parent 保留 parent ?”

这是什么意思?像这样:

Class *grandparent = [[Grandparent alloc]init];
Class *parent = [grandparent copy];
..............

有人帮我...

最佳答案

即使祖 parent 释放 parent 。仍然会导致父子之间的保留循环。

除非有其他对父项或子项的引用,否则它们都会变成悬垂的。但是父子之间的保留循环阻止了其中任何一个被释放,并且它们变成了浪费的内存。

child 不应该保留 parent 。如果有的话,请在子项中使用弱引用来维护对父项的引用。

关于objective-c - 什么是 "parent"和 "child"对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21495818/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com