2
votes

I got this image from Apple docs: enter image description here

Sorry for this stupid question. Someone can give an explanation of this image?. My interpretation:

  1. We create an instance of an Object of class A.
  2. After [[ClassA alloc]init] the object has a retain count of 1.
  3. After this, we create an instance of ClassB and add him to Class A as an iVar ¿it is correct? Instantiation means a retain count of 1, and the relation of ownership (now A owns B) means an increment of 1 of the retain count. (1+1 = 2) Now B has a retain count of 2. ¿it is correct? Anynone?
3
I believe the chart is talking about a single object created by Class A and used by Class B and Class C.rmaddy
Makes sense. So you say, we create A, we add A to B as an iVar, then we copy C and add it to the B instance (that contains A), then we send a release method to A .... ¿it is correct?joan
No. An instance of A creates the object X. Then an instance of B retains an instance of the object X. Then an instance of C copies the object X.rmaddy
It's pretty simple. Any method who's name begins with alloc or new or copy will return an object with a retain count of 1. Any call to retain will increase the retain count by 1, release will decrease the retain count by 1, and autorelease will decrease the retain count next time the "current" pool is released (typically when the main thread is idle next). All other methods have no impact on the retain count at all — if they create an object it will be 1 but autoreleased to 0. If the retain count hits 0 the object may be destroyed (this is not guaranteed. do not rely on it)Abhi Beckert
With regard to ivars, if the @property definition defines it as strong or retain then the value will be retained when it's stored in the property and released when it's removed from the property. If it's weak or assign or unsafe_unretained then it will not be retained. If a property just has a set method but no @property definition you need to check the documentation but usually it will behave the same as strong.Abhi Beckert

3 Answers

1
votes

You've misunderstood the diagram. Going from left to right:

  • An instance of Class A creates a new object - it has a retain count of one.
  • An instance of Class B retains the new object - it now has a retain count of 2.
  • An instance of class C makes a copy of the new object - this is another new object, with a retain count of one.
  • The instance of Class A releases the new object - it now has a retain count of 1, so is not deallocated.
  • Class C releases its copy - the copied instance is deallocated
  • Class B releases its reference to the new object. The reference count returns to zero and it is deallocated.
1
votes

1) We create an instance of an Object of class A. FALSE

ClassA creates an instance of just another class, let's call it ClassZ.

2) After [[ClassA alloc]init] the object has a retain count of 1. FALSE

It is not [[ClassA alloc]init], it is [[ClassZ alloc]init]. Now the instance of ClassZ has retain count of 1.

3) After this, we create an instance of ClassB and add him to Class A as an iVar FALSE

An instance of ClassB just retains the instance of ClassZ created on step 1. And retain count of the instance of ClassZ becomes 2.

4) Instantiation means a retain count of 1, and the relation of ownership (now A owns B) means an increment of 1 of the retain count. (1+1 = 2) Now B has a retain count of 2. FALSE

Here it is not important who created ClassB and what its retain count at the moment. It is just another object claiming ownership on the instance of ClassZ.

1
votes

So, is really simple, follow this points whose explain the main line:

1- An instance of class A allocs and initializes an object of class X --> X retain count = 1;

2- An instance of class B retain X. It is not important how B obtain X. The important is that want have the ownership of the object to maintains it alive --> X retain count = 2;

3- A release X --> X retain count = 1;

4- B release X --> X retain count = 0; --> X is deallocated

The second part instead is just to explain that copying an object, another object is allocated, and so, the same mechanism on this new object has not effects on the first object.