Join the Stack Overflow Community
Stack Overflow is a community of 6.4 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

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 ?
share|improve this question
up vote 9 down vote accepted

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() 
    
  2. After vc2 refers to the same object as vc1. The reference count of object turns to 2

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

    var vc3:UIViewController? = vc2
    
  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
    

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.

share|improve this answer
2  
why CFGetRetainCount giving 4 then @KrishnaCA? – Sahil 9 hours ago
1  
5  
@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 :) – CodaFi 8 hours ago
1  
@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. – CodaFi 8 hours ago
1  
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. – CodaFi 8 hours ago

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

share|improve this answer
    
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 – fpg1503 8 hours ago
    
@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 – KrishnaCA 7 hours ago
    

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.

share|improve this answer

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.