“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.
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 ?
Bar() will be released as soon as start(:)'s finishes execution. As there
is no strong reference to Bar() and the closure runs inside of function
start(:).
steps:
Bar() created
execute Bar().start(:) with {print(self.object)}
pass closure {print(self.object)} to start(:)
closure runs inside of function start(:)
closure released after its execution
start(:) finished
Bar() released
Zhaoxin
···
On Sat, Jul 2, 2016 at 6:06 AM, Grzegorz Leszek via swift-users < swift-users@swift.org> wrote:
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.”