# Does inout affect reference count on a class?

**URL:** <https://forums.swift.org/t/does-inout-affect-reference-count-on-a-class/41061>\
**Category:** Using Swift\
**Tags:** arc\
**Created:** [October 9, 2020, 10:55pm UTC](https://forums.swift.org/t/does-inout-affect-reference-count-on-a-class/41061 "2020-10-09T22:55:01Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![1oo7](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/1oo7/32/17598_2.png) [@1oo7](https://forums.swift.org/u/1oo7)\
**Post date:** [October 9, 2020, 10:55pm UTC](https://forums.swift.org/t/does-inout-affect-reference-count-on-a-class/41061/1 "2020-10-09T22:55:01Z")

</div>

What is the difference between passing an instance of a class into a function with `inout` as opposed to without `inout`?

E.g.:

```swift
func doSomething(with viewController: UIViewController) {
...
}

```

vs.

```swift
func doSomething(with viewController: inout UIViewController) {
...
}

```

What is the difference if any here, especially in terms of ARC?

---

<div class="post-metadata">

**Author:** ![Jumhyn](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jumhyn/32/3891_2.png) [@Jumhyn](https://forums.swift.org/u/Jumhyn)\
**Post date:** [October 9, 2020, 11:09pm UTC](https://forums.swift.org/t/does-inout-affect-reference-count-on-a-class/41061/2 "2020-10-09T23:09:37Z")

</div>

I don't believe that there are any guarantees made in terms of reference _count_ (since the optimizer may remove retain/release pairs that are provably unnecessary), but both forms of `doSomething` hold a strong reference to `viewController`. The difference is whether `viewController` itself can be reassigned:

```swift
class C {
  var x: Int
  init(x: Int) { self.x = x }
}

func doSomething(_ c: C) {
  c.x += 1
}

func doSomethingInout(_ c: inout C) {
  c = C(x: c.x + 1)
}

```

with `inout` on a reference type, we are allowed to reassign the entire instance to something else. When used, we would observe this behavior:

```swift
let c1 = C(x: 0)
let c2 = c1
var c3 = C(x: 0)
var c4 = c3

doSomething(c1) // OK
c1 === c2 // true
doSomethingInout(&c2) // Error!

doSomething(c3) // OK
c3 === c4 // true
doSomethingInout(&c3) // OK
c3 === c4 // false

```

---

<div class="post-metadata">

**Author:** ![David\_Smith](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/david_smith/32/37508_2.png) [@David\_Smith](https://forums.swift.org/u/David_Smith)\
**Post date:** [October 9, 2020, 11:13pm UTC](https://forums.swift.org/t/does-inout-affect-reference-count-on-a-class/41061/3 "2020-10-09T23:13:18Z")

</div>

Unfortunately it's relatively difficult to say in isolation, because it can depend on surrounding context, the contents of the functions, and how the compiler can transform things in various ways as it optimizes.

If I had to guess in isolation, neither will immediately do a retain, because the first one will pass `viewController` at +0, and the second one the ARC optimizer will probably successfully optimize the copy-in-copy-out.

---

<div class="post-metadata">

**Author:** ![1oo7](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/1oo7/32/17598_2.png) [@1oo7](https://forums.swift.org/u/1oo7)\
**Post date:** [October 9, 2020, 11:16pm UTC](https://forums.swift.org/t/does-inout-affect-reference-count-on-a-class/41061/4 "2020-10-09T23:16:04Z")

</div>

Interesting, thanks.

Here's an example playground that shows the `inout` one had a lower retain count.

```swift
import SwiftUI
import Foundation

struct MyView: View {
    var body: some View {
        Text("Hello")
    }
}

var host = UIHostingController(rootView: MyView())

print(host)

func doSomething<T>(with host: UIHostingController<T>) {
    print(host)
    print(CFGetRetainCount(host)) // will print 3
}

func doSomethingElse<T>(with host: inout UIHostingController<T>) {
    print(host)
    print(CFGetRetainCount(host)) // will print 2
}

doSomething(with: host)
doSomethingElse(with: &host)

```

---

<div class="post-metadata">

**Author:** ![David\_Smith](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/david_smith/32/37508_2.png) [@David\_Smith](https://forums.swift.org/u/David_Smith)\
**Post date:** [October 9, 2020, 11:18pm UTC](https://forums.swift.org/t/does-inout-affect-reference-count-on-a-class/41061/5 "2020-10-09T23:18:00Z")

</div>

Passing it to CFGetRetainCount() may actually be causing ARC to insert a retain in that case. Also, playgrounds can't reliably be used for this sort of thing because they run extra code on every line.

The only _accurate_ ways to do this are:

- lldb breakpoints on the various swift\_retain/swift\_release functions
- reading the generated assembly code

Working on stdlib performance I do a lot of reading assembly code \>.\<

Also remember to compile with optimizations enabled
