简体   繁体   English

在Objective-C中如何处理参考副本?

[英]how reference copy is handled in Objective-C?

Object graph 对象图

[Instance A]
         tree
       /       \
      /         \
     /           \
    ↓             ↓
[Instance B]     [Instance C]
 apple              bug

Question

Instance A has to reference copies to Instance B and Instance C . 实例A必须将副本引用到实例B实例C。

If I retain or release Instance A , which has references to the other two instances, what happens to the various reference counts? 如果我保留或释放具有对其他两个实例的引用的实例A ,那么各种引用计数将如何处理?

When you retain or release A, only its reference count changes. 当您保留或释放A时,仅其引用计数会更改。 What happens to B and C depends on your model and implementation. B和C发生什么情况取决于您的模型和实现。

If A "owns" or needs to keep B/C around, it should retain it at some point (independent of A itself being retained), and release it when A is being deallocated. 如果A“拥有”或需要保留B / C,则应在某个时候保留B / C(独立于保留A本身),并在释放A时释放它。

If you're not implementing A, you need to check the documentations to see whether it owns B/C or you need to explicitly retain and release them. 如果您没有实现A,则需要查看文档以查看其是否拥有B / C,或者需要显式保留并释放它们。

I checked your original question, not sure if this is the answer you're looking for. 我检查了您原来的问题,不确定是不是您要的答案。 If not, explain it a bit... 如果没有,请解释一下...

You do not care about retain counts . 您不在乎保留计数 No, honestly you don't. 不,说实话你不会。 You only care about whether A owns B and C (or more accurately, has a share in the ownership of B and C). 您只在乎A是否拥有B和C(或更准确地说,拥有B和C的所有权)。

So presumably somewhere you have a method that sets the children of A (it might be A's designated initialiser). 因此,大概在某个地方,您可以使用一种方法来设置A的子代(可能是A的指定初始化程序)。 If that method retains B and C then A must release B and/or C when it no longer needs ownership. 如果该方法保留了B和C,则A在不再需要所有权时必须释放B和/或C。 This will be in two circumstances: 在两种情况下:

  1. when B and/or C are to be overwritten with new children 当B和/或C被新的孩子覆盖时
  2. When A is about to be deallocated 当A即将被释放时

Anyway, having said that, to answer your question: 话虽如此,回答您的问题:

If you retain A it has no effect on the retain counts of B and C. 如果保留A,则对B和C的保留计数没有影响。

If you release A it has no effect on the retain count of B and C unless nobody else has ownership of A. In this case A's dealloc will be invoked which will release both B and C. 如果释放A, 除非其他人都不拥有A的所有权, 否则它对B和C的保留数没有影响。在这种情况下,将调用A的dealloc,这将释放B和C。

When you call the retain or release on objectA nothing will happen with the objects which hold the the reference of objectA. 当您调用对objectA的保留或释放时,持有objectA引用的对象将不会发生任何事情。 (Unless you have overridden these methods.)(retain increases retain count and release decreases retain count) (除非您重写了这些方法。)(保留增加保留计数,释放减少保留计数)

Incase of release, if the retain count becomes 0, dealloc will get called and objectA is released. 在释放的情况下,如果保留计数变为0,则将调用dealloc并释放objectA。 And you are responsible for releasing the allocated objects of the class. 您负责释放该类的已分配对象。 The other objects which still hold the reference of objectA become dangling pointers. 仍然保持对objectA的引用的其他对象成为悬空指针。

Regards, Dhana. 尊敬的达纳。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM