Resolving Leaky Closures in Swift

Resolving Leaky Closures in Swift

Closures are self-contained blocks of functionality that can be passed around and used in your code. They are similar to blocks in C and Objective-C and lambda functions in other programming languages. Closures can capture and store references to any constants and variables from the context in which they are defined. This is known as closing over those constants and variables.

 

Closures is a powerful feature in Swift, but it does demand caution in some cases.

Classes and Closures are both reference types. Because a closure can capture values from its surrounding scope, a closure can create a reference cycle.

 

For example:

class User {
    let firstName: String
    let lastName: String
    lazy var fullName: () -> String = {
    return "\(self.firstName) (\(self.lastName))"
    }

init(firstName: String, lastName: String) {
    self.firstName = firstName
    self.lastName = lastName
    }

deinit {
    print("Deallocated User")
    }
}

var newUser: User? = User(firstName: "John", lastName: "Doe")

user?.fullName()

user = nil

As we know, all references declared in Swift are by default strong references. The newUser instance of the User class, holds a strong reference to the Closure stored in the fullName property. And, the Closure stored in the fullName property also holds a strong reference to the newUser instance i.e. self. This results in what we call as a strong reference cycle. Being a cyclic reference, the deinit of the class is never involved, and the ARC will never remove the reference from memory thus leading to a memory leak.

 

This is a typical situation that demands caution whilst using Closures. To resolve this kind of a problem we can use a Capture List…

 

Defining a Capture List

The references a Closure holds to reference types are strong by default. We can change this behaviour by defining a capture list. The capture list determines how values used inside the closure should be captured.

 

Weak capturing

A weak reference type keeps a weak reference to the instance it references. This means that the reference to the instance will not be accounted for by the ARC. A weak instance is deallocated if there exists no other strong reference to the instance.

class User {
    let firstName: String
    let lastName: String
    lazy var fullName: () -> String = { [weak self] in
    return "\(self.firstName) (\(self.lastName))"
    }
}

In the above code, the “weak self” defines the reference to the self within the closure, as a weak reference, and hence the cyclic reference is avoided.

Unowned capturing

Unowned references are similar to weak references in that they don’t keep a strong reference to the instance they are referencing. The difference is unowned reference is always expected to have a value.

Title

class User {
    let firstName: String
    let lastName: String
    lazy var fullName: () -> String = { [unowned self] in
    return "\(self.firstName) (\(self.lastName))"
    }
}

The above 2 examples show how Capture list can be used to avoid reference cycles and hence memory leaks in closures.