22

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 ?

4 Answers 4

21

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

Sign up to request clarification or add additional context in comments.

13 Comments

why CFGetRetainCount giving 4 then @KrishnaCA?
@Sahil CFGetRetainCount returns 4 because @KrishnaCA misunderstands the intent of variable assignment with respect to reference counts. In this example, refcounts are incremented not because of the assignments in and of themselves, but because the first three assignments are to a strong reference. The assignment to a weak reference still performs a temporary increment of the reference count (in particular, it invokes _swift_weakInit). This is all implementation defined, tho. You shouldn't have to think about it :)
@KrishnaCA Sure. Take a look around HeapObject.cpp for the set of calls we can emit for this stuff. The temporary increment is an implementation detail and can be flattened away if the optimizer can guarantee the lifetime of each of these references.
Even if you (first change the types, then) set them to nil, the semantics of ARC are such that we don't have to honor them with a decrement. That it happens at all is, again, implementation defined! ARC is a whole lotta fun now that we don't have to carry around all the dumb baggage ObjC's calling convention required.
|
5

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

2 Comments

CFGrtRetainCount does return 4 but you're not answering what's been asked. Take a look at CodaFi's comments to see why your answer is incorrect
@Sahil, try getting retain count for each step here once and check the result. I just tried it. The retain count if vc1 after var vc1 = UIViewController() is 2. Please confirm
2

In my opinion vc1 to vc3 increases the retain count and the by default property is strong until we specify those as weak.

strong: Strong is generally used by a class to establish ownership of an object. It increases the retain count (something ARC deals with for you), it basically keeps the object that is pointed to in memory until that class instance stops pointing to it. This is usually what you want, but there it can cause something called a “retain cycle.”

In case of vc4 as you dec as weak:

weak: This gives a pointer to an object, but does not claim ownership, and does not increase the retain count. It basically keeps a valid pointer to an object as long as another class points to it strongly. If nothing else is trying to retain it, the weak pointer is automatically set to nil.

Comments

1

1,2,3,4 - reference count will be 3

The only exception when reference count won't be incremented - 4th line, because of weak modificator

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.