Jacob’s Tech Tavern

Jacob’s Tech Tavern

The Case Against [unowned self]

Why you shouldn’t ever use this keyword (and when you should)

Jacob Bartlett's avatar
Jacob Bartlett
Jan 08, 2024

unowned references help you to prevent retain cycles in ARC. They offer a Faustian bargain: a sliver of runtime performance in exchange for the risk of crashing if you get it wrong


Abstract representation of “the heap” — photo by Rick Mason on Unsplash

Every time you use a class, closure, or actor, Swift is storing information on the heap. The variable you’re passing around is really a pointer to this address in memory (a.k.a. a reference).

Back in the olden days, Mac and iOS developers would need to manage this memory manually through functions like alloc() to create a block of memory on the heap, retain() to add another reference, and release() to free up the memory.

In 2011, Apple introduced ARC — automatic reference counting — which made the compiler write this boilerplate memory management code itself, saving untold centuries of cumulative dev-time.

ARC introduced the concept of different types of reference.


Strong references

This is the default reference; a pointer which makes sure the memory it points at remains alive while the pointer is in use.

Creating a strong reference increments a heap object’s reference count (a.k.a. refCount), and the object is deallocated, freeing up the memory in the heap, when refCount hits zero.


Weak references

weak references allow heap objects to point at each other without making a retain cycle. Developers can hence create references to classes in closures and delegates without keeping them alive unnecessarily.

weak references point at the heap object in memory without incrementing refCount. If refCount dips to zero, the object is deallocated and the weak reference becomes nil— this prevents memory leaks in closures.

To handle this modus operani, weak references always come wrapped in an optional.

// StoreViewModel.swift

func loadStorefront() {
    api.fetchInventory() { [weak self] inventory in 
        self?.inventory = inventory
    }
}

In this example, inventory is fetched over the network, and the closure callback runs when data arrives. self is weakly captured, meaning that the properties on StoreViewModel can be updated in the closure. If the user leaves the screen, and the view model’s strong refCount becomes zero, then the runtime can deinitialize the view model — it won’t be kept alive in memory while waiting for the callback, and so nothing happens when inventory is returned.


Unowned references

A third type of reference was introduced with Swift to help prevent retain cycles: unowned. This behaves similarly to a weak reference, except it assumes that the memory of the referenced object will always outlive the unowned pointer referencing it.

// StoreSingleton.swift

static let sharedInstance = StoreSingleton()

private init() {}

func configureStorefront() {
    api.fetchInventory() { [unowned self] inventory in 
        self.inventory = inventory
    }
}

Here, we have a “true singleton” (with a private init), so we can be sure the lifetime of the object will outlive any closures. The [unowned self] in the closure capture list prevents a retain cycle from being created (though, for a singleton, we don’t actually need to worry about retain cycles).

The closure is a little bit simpler, and unowned comes with a few performance benefits compared to weak references:

  • There is no need to use optional chaining or unwrapping operations.

  • unowned references store less metadata as they don’t create a side table on the heap object.

  • Accessing the memory pointed at by weak references involves one extra jump, or layer of indirection, between pointers.

This performance comes with a catch though: if you get the lifetimes wrong, and the closure or property outlives the unowned reference, your app will crash.

There is actually also a 4th type, the unowned(unsafe) reference, which disables runtime safety checks. Instead of crashing if the referenced object is deallocated, the program will happily accessing the memory address anyway. If crashes are the second-worst thing you do to your users, corrupting user data is #1.


Is unowned worth it?

Obviously, crashes are pretty bad. And premature optimisation, as Knuth is frequently misquoted, is a temptation about which we should be mindful. So we shouldn’t use unowned references willy-nilly just to get a little bit more performance.

If you’re reading my blog, you probably know what you are doing when it comes to Swift. But can you be certain this is true for everybody on your team?

Can you be sure every code review you perform at 5pm on a Friday is judicious about understanding the many-branching code paths that lead to object deinstantiation, every time you spot the unowned keyword?

Ultimately, unowned isn’t a programming problem. It’s a human problem.

Subscribe for free to continue reading

Enter your email and get every post from Jacob’s Tech Tavern.
Already have an account? Sign in
© 2026 Jacob Bartlett · Privacy ∙ Terms ∙ Collection notice
Start your SubstackGet the app
Substack is the home for writing, podcasting, and media founders.