<html><head><meta http-equiv="Content-Type" content="text/html charset=us-ascii"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><br class=""><div><blockquote type="cite" class=""><div class="">On Dec 8, 2015, at 7:49 AM, Joe Groff <<a href="mailto:jgroff@apple.com" class="">jgroff@apple.com</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><meta http-equiv="Content-Type" content="text/html charset=us-ascii" class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">Capturing weak by default is not safe. ARC optimization will shrinkwrap the lifetimes of strong references to their last strong use, so capturing a local variable that references a fresh object would cause the object to immediately go away:<div class=""><br class=""></div><div class="">let x = NSObject()</div><div class="">dispatch_sync {[weak x] in x!.foo() } // x can already be gone by this point</div><div class=""><br class=""></div><div class="">There's no one right answer here.</div></div></div></blockquote><br class=""></div><div>One reason cycles are so prevalent with closures is that it's easy to capture too much, since referencing 'self.foo' always captures the entirety of 'self'. If the properties of 'self' you need are immutable, or you don't need to see their mutations, you can reduce the risk of cycles by capturing those properties explicitly, turning this:</div><div><br class=""></div><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class=""><div>self.setCallback { doStuffWith(self.zim, self.zang) }</div></blockquote><div><br class=""></div><div>into:</div><div><br class=""></div><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class=""><div>self.setCallback {[zim, zang] in doStuffWith(zim, zang) }</div><div><br class=""></div></blockquote>For 'let' properties of classes, it'd be reasonable to propose having closures capture the *property* directly by default in this way instead of capturing 'self' (and possibly allowing referencing them without 'self.', since 'self' wouldn't be involved in any cycle formed this way).<div class=""><br class=""></div><div class="">-Joe<br class=""><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class=""><div><blockquote style="margin: 0px 0px 0px 40px; border: none; padding: 0px;" class=""><div class=""><br class=""></div></blockquote></div></blockquote></div></body></html>