<div dir="ltr"><div class="gmail_default" style="font-family:georgia,serif">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(:).</div><div class="gmail_default" style="font-family:georgia,serif"><br></div><div class="gmail_default" style="font-family:georgia,serif">steps:</div><div class="gmail_default" style="font-family:georgia,serif">Bar() created</div><div class="gmail_default" style="font-family:georgia,serif">execute Bar().start(:) with {print(self.object)}</div><div class="gmail_default" style="font-family:georgia,serif">pass closure {print(self.object)} to start(:)</div><div class="gmail_default" style="font-family:georgia,serif">closure runs inside of function start(:)</div><div class="gmail_default" style="font-family:georgia,serif">closure released after its execution</div><div class="gmail_default" style="font-family:georgia,serif">start(:) finished</div><div class="gmail_default" style="font-family:georgia,serif">Bar() released</div><div class="gmail_default" style="font-family:georgia,serif"><br></div><div class="gmail_default" style="font-family:georgia,serif">Zhaoxin</div><br></div><div class="gmail_extra"><br><div class="gmail_quote">On Sat, Jul 2, 2016 at 6:06 AM, Grzegorz Leszek via swift-users <span dir="ltr"><<a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hello Swift Community,<br>
<br>
According to the Swift Programming Language Book:<br>
<br>
“A strong reference cycle can also occur if you assign a closure to a<br>
property of a class instance, and the body of that closure captures<br>
the instance.”<br>
<br>
Excerpt From: Apple Inc. “The Swift Programming Language.” iBooks.<br>
<a href="https://itun.es/pl/jEUH0.l" rel="noreferrer" target="_blank">https://itun.es/pl/jEUH0.l</a><br>
<br>
Does it mean that example below is valid? ( no need to use capture list ):<br>
<br>
import Swift<br>
<br>
class A {}<br>
<br>
class Foo {<br>
var object = A()<br>
func bar() {<br>
Bar().start {<br>
print(self.object)<br>
}<br>
}<br>
}<br>
<br>
class Bar {<br>
func start(completion: () -> ()) {<br>
completion()<br>
}<br>
}<br>
<br>
As far as I understand, Leak will occur when class Bar stores<br>
completion in the property. But are there any other edge cases ?<br>
<br>
Regars,<br>
Greg Leszek<br>
_______________________________________________<br>
swift-users mailing list<br>
<a href="mailto:swift-users@swift.org">swift-users@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-users" rel="noreferrer" target="_blank">https://lists.swift.org/mailman/listinfo/swift-users</a><br>
</blockquote></div><br></div>