[swift-users] Strong Reference Cycle

Grzegorz Leszek grzesiek.leszek at gmail.com
Fri Jul 1 17:06:08 CDT 2016


Hello Swift Community,

According to the Swift Programming Language Book:

“A strong reference cycle can also occur if you assign a closure to a
property of a class instance, and the body of that closure captures
the instance.”

Excerpt From: Apple Inc. “The Swift Programming Language.” iBooks.
https://itun.es/pl/jEUH0.l

Does it mean that example below is valid? ( no need to use capture list ):

import Swift

class A {}

class Foo {
    var object = A()
    func bar() {
        Bar().start {
            print(self.object)
        }
    }
}

class Bar {
    func start(completion: () -> ()) {
        completion()
    }
}

As far as I understand, Leak will occur when class Bar stores
completion in the property. But are there any other edge cases ?

Regars,
Greg Leszek


More information about the swift-users mailing list