Swift: How ARC deals with anonymous objects?

74 views Asked by At

ARC came many years ago to replace painful MRC, but since ARC is only a compiler technique its magic happens during the compile time. So, basically what it does - automatically inserts retain and release calls where appropriate. So, now comes my question - how ARC deal with the following examples

func foo() {
 X().doSomething()
}

func bar() {
 anotherFunc(X())
}

Objects are created with ref count == 1(as far as I know it, at least in old MRC objc alloc, new, copy where calling retain. Please correct me if I'm wrong), so the compiler have to decrease ref count on these objects in order to dealloc them ? But how is this done since there is actually no ref to these objects, they are anonymous ! I assume that compiler may do the following optimisation

func foo() {
 let var0001 = X()
 var0001.doSomething()
 var0001.release()
}

I mean that compiler will make the reference to the anonymous object so it will be able to call release on it. But, I'm not sure that this is exactly how it works! So, can somebody explain please what actually happens in this scenario ?
Kind Regards,
Andre

0

There are 0 answers