Kotlin Multiplatform, from the compiler up · Puzzle 8 · 9 of 16
Weak references and closures
Puzzle 7 left Swift with a leak it can't see: two objects holding each other. Swift's fix is a reference that doesn't count. Here's how it works, and the most common way the cycle sneaks in: a lambda.
The problem
A screen holds a timer helper; the helper keeps a lambda, Swift calls it a closure, to update the screen every second; the closure uses the screen. The user leaves the screen, and it's never freed: its deinit never runs. From puzzle 7 you know why: a cycle. How do Swift developers break it?
See it
A screen stores a closure that refreshes it every second.
Step 1 of 4The closure uses
self, so it holds the screen strongly: a cycle. The user leaves; neither is freed.Step 2 of 4[weak self]makes the closure's link a weak reference: it doesn't count. Predict: what happens when the user leaves now?Step 3 of 4The screen's count reaches zero: it's freed and its deinit runs. Inside the closure,
selfis now nil.Step 4 of 4
The building block
A Swift class can have a deinit: code that runs when an instance is freed. A missing deinit message is the classic sign of a leak.
A weak reference points at an object without adding to its count. It can't keep the object alive; when the object is freed, the weak reference becomes nil (Swift's null). Make one link in a cycle weak, and the counts can reach zero again:
class Apartment {
weak var tenant: Person? // doesn't keep the person alive
}
The sneakier cycle is a closure, Swift's lambda. A closure holds strong references to everything it uses, including self (Swift's this). If self stores the closure, that's a cycle:
class Screen {
var onTick: (() -> Void)?
func start() {
onTick = { [weak self] in // capture self weakly
self?.refresh()
}
}
}
[weak self] is a capture list: it tells the closure to hold self weakly. Inside, self may already be gone, so it's optional: self?.refresh().
Which link should be weak? The one pointing at the object that should be allowed to go first: here, the screen.
The analogy
Two climbers roped together hold each other up forever. A weak reference is a hand on the other's shoulder instead of a rope: you still know where they are, but if they leave, nothing holds them back.
Check yourself
Sources
- The Swift Programming Language: Automatic Reference Counting ("Weak and unowned references enable one instance in a reference cycle to refer to the other instance without keeping a strong hold on it"; "Use a weak reference when the other instance has a shorter lifetime"; strong reference cycles for closures are resolved with "a closure capture list"; the
weak var tenant: Person?example)
What you now know
A weak reference points at an object without adding to its count, so it can't keep it alive; it becomes nil when the object is freed. Make one link in a cycle weak and the cycle can be freed. Closures, Swift's lambdas, hold strong references to what they use, including self, so a closure stored by the object it captures is a cycle; [weak self] makes that capture weak. deinit runs when an object is freed.
Answer every question above correctly to unlock the next puzzle.
Something wrong or unclear on this page? Tell me: a puzzle that didn't click is a bug in the puzzle.