Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reference count in ARC

Tags:

I have little bit confusion in ARC reference count can you please tell me what will be reference count of bellow code.

var vc1 = UIViewController()
var vc2 = vc1
var vc3 = vc2
weak var vc4 = vc3

Question is what will be the:

  • reference count of vc1 ?
  • reference count of vc2 ?
  • reference count of vc3 ?
  • reference count of vc4 ?
like image 456
jignesh Vadadoriya Avatar asked Nov 25 '16 06:11

jignesh Vadadoriya


2 Answers

Here, vc1, vc2, vc3 refer to the same object. So, the reference count of that object is 3. When vc4 refer to the same object, since it is weak reference, the reference count will not be incremented by 1. So, the reference count after this will also be 3

  1. The reference count of UIViewController object that is created and referred by vc1 after first line of code is 1.

    var vc1:UIViewController? = UIViewController() // strong reference 
    
  2. After vc2 refers to the same object as vc1. The reference count of object turns to 2

    var vc2:UIViewController? = vc1 // strong reference
    
  3. After vc3 refers to the same object as vc1 and vc2. The reference count of object turns to 3

    var vc3:UIViewController? = vc2 // strong reference
    
  4. After vc4 refers to the same object as vc1, vc2 and vc3. Since vc4 is weak reference, the reference count will not be incremented. That means the count is still 3.

    weak var vc4:UIViewController? = vc3 // weak reference
    

What it means:

Execute the following code.

   vc1 = nil; // reference count = 3-1 = 2
   vc2 = nil; // reference count = 2-1 = 1
   vc3 = nil; // reference count = 1-1 = 0 and object is destroyed

Now, print the value of vc4. It will be nil. This happens because the reference count of object turns to zero and all of the variables refers to same object.

Edit:

Using CFGetRetainCount in the below code gives the following results as stated here:

var vc1:NSDate? = NSDate()
print(CFGetRetainCount(vc1)) // 2 - I expected this to be 1 as only one variable is strongly referring this object. 

var vc2:NSDate? = vc1
print(CFGetRetainCount(vc1)) // 3 - reference count incremented by 1 (strong reference)

var vc3:NSDate? = vc2
print(CFGetRetainCount(vc3)) // 4 - reference count incremented by 1 (strong reference)

weak var vc4:NSDate? = vc1
print(CFGetRetainCount(vc1)) // 4 - reference count not incremented (weak reference)

vc1 = nil
print(CFGetRetainCount(vc2)) // 3 - reference count decremented by 1 (strong reference removed)

vc2 = nil
print(CFGetRetainCount(vc3)) // 2 - reference count decremented by 1 (strong reference removed)

vc3 = nil 
print(vc4) // nil - reference count should be decremented by 1 (last strong reference removed)

// Also due to the final line vc3 = nil, reference count should become zero
// However, we can't use `CFGetRetainCount` to get reference count in this case
// This is due to the final strong reference being removed and object getting destroyed

The reason why CFRetainCount is giving 2 in the 1st line has been discussed here. Thanks @CodaFi and @Sahil for your discussion in comments

like image 96
KrishnaCA Avatar answered Sep 21 '22 08:09

KrishnaCA


You can use CFGetRetainCount function for check ref count.

var vc1 = UIViewController()
var vc2 = vc1
var vc3 = vc2
weak var vc4 = vc3


print(CFGetRetainCount(vc1)) //4
print(CFGetRetainCount(vc2)) //4 
print(CFGetRetainCount(vc3)) //4
print(CFGetRetainCount(vc4)) //4

you can also refer this Get Ref Count

like image 28
Sahil Avatar answered Sep 21 '22 08:09

Sahil