Closures can keep objects alive by holding strong references to them, causing memory to not be freed. This is called a retain cycle.
Closures causing retain cycles in Swift
class MyClass { var closure: (() -> Void)? func setup() { closure = { print(self) } } }
Closures capture variables from their surrounding context, including self.
If self holds a strong reference to the closure, and the closure captures self strongly, a retain cycle happens.
closure captures self strongly, and self owns closure.class MyClass { var closure: (() -> Void)? func setup() { closure = { print(self) } } }
[weak self] breaks the retain cycle by capturing self weakly inside the closure.class MyClass { var closure: (() -> Void)? func setup() { closure = { [weak self] in guard let self = self else { return } print(self) } } }
[unowned self] also breaks the retain cycle but assumes self will not be nil when closure runs.class MyClass { var closure: (() -> Void)? func setup() { closure = { [unowned self] in print(self) } } }
This example shows how to use a weak reference in a closure to avoid a retain cycle. When person is set to nil, the object is freed properly.
class Person { let name: String var greeting: (() -> Void)? init(name: String) { self.name = name } func sayHello() { greeting = { [weak self] in guard let self = self else { return } print("Hello, my name is \(self.name)") } } } var person: Person? = Person(name: "Alex") person?.sayHello() person?.greeting?() person = nil print("Person is now nil")
Always use [weak self] or [unowned self] in closures that capture self to avoid retain cycles.
Use weak when self can become nil during closure lifetime.
Use unowned when you are sure self will not be nil when closure runs.
Closures capturing self strongly can cause retain cycles and memory leaks.
Use capture lists like [weak self] or [unowned self] to break retain cycles.
Understanding retain cycles helps keep your app memory efficient and bug-free.