Saran
Saran

Reputation: 6392

Does ARC set its reference type instance properties to nil before deallocation?

This question occured to me while reading this.

My question is in reference to the image below:

enter image description here

Once john is set to nil, Person instance no longer has any more strong reference and hence will be deallocated. But Apartment has two strong references and one of which is by the property on Person instance that would be soon deallocated. I believe, this strong reference continue to remain after deallocation and goes out of reach by the code.

So, setting unit14A to nil will remove only one strong reference to Apartment instance and it should not be deallocated as there would be one more strong reference due to the above case.

But, as the document says Apartment instance promptly got deallocated. To me, this can only happen if at the time of Person instance deallocation it sets its apartment property to nil, by that removing that strong reference on Apartment instance. But I couldn't find any documentation to verify this.

So, How does the Apartment instance get deallocated? What happened to the strong reference from the Person instance apartment property?

Can someone help me to understand this?

Upvotes: 3

Views: 198

Answers (4)

gnasher729
gnasher729

Reputation: 52538

Obviously not before deallocation, but during deallocation.

When an object's reference count goes to zero, the deallocation process starts. The object is marked as "being deallocated". At that point, the object will die (unlike Java, where it can be recovered). If an object is marked like this, it cannot be assigned to weak references (they stay nil), or to strong references.

Then dealloc is called, that is the dealloc methods that you have written. After that, strong references are set to nil, reducing their reference counts, then associated objects are removed, and finally the memory for the object is deleted.

Upvotes: 0

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726539

Objective-C objects are reference counted, meaning that for each object, the system keeps track of how many other objects hold a reference to it. This is object's reference count. Two special messages, retain and release, are used to maintain the reference count behind the scene. Once reference count goes down to zero, the system deallocates the object.

ARC provides "magic" to make reference counting work in a declarative way. The compiler knows every strong reference in your code, so when you do this

myStrongRef = nil;

the compiler quietly inserts a call to release in front of the assignment:

[myStrongRef release];
myStrongRef = nil;

To me [deallocation of Apartment] can only happen if at the time of Person instance deallocation it sets its apartment property to nil, by that removing that strong reference on Apartment instance.

Setting a strong reference to nil one way of breaking a strong reference. It is sufficient, but it isn't necessary. The important thing about setting a strong reference to nil is not the act of setting itself, but what happens immediately before it: the instance referred to by the strong reference gets a release message, instructing it to decrement its reference count. That is precisely what ARC does behind the scene for you: it sends the release message to Apartment, without setting Person's reference to nil.

How does the Apartment instance get deallocated? What happened to the strong reference from the Person instance apartment property?

Once strong reference from Person has sent its release message to Apartment, that strong reference disappears. The actual pointer may be set to Apartment's address, but nobody cares about it, because the Person itself is unreachable.

Upvotes: 5

zaph
zaph

Reputation: 112857

The life of an object depends on it's reference count, not any actual pointer to the object.

Strong reference is a way of speaking, there is no difference between a strong and weak reference, they are just pointers. The difference is that when a strong reference is created the reference count of the object pointed to in incremented and when deleted the reference count is decreased. When an object's reference count would become zero the object is deallocated.

Upvotes: 2

CRD
CRD

Reputation: 53000

Your intuition is correct. When an object is being deallocated under ARC all the strong references it holds are first relinquished - essentially they are set to nil, but in practice the implementation may differ.

This is also what happens when a method returns, or a block of code containing declarations exits, all the strong references held in local variables are relinquished.

All the details can be found in the Clang documentation.

HTH

Upvotes: 1

Related Questions