<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">I got bit again by a sneaky memory leak concerning local functions and would like to discuss a small language change. I vaguely remember this being discussed in the past, but can’t find the thread (if anybody could point me to it, I’d appreciate it). Basically, here’s an example of the leak:<div class=""><br class=""></div><div class=""><pre style="box-sizing: border-box; font-family: SFMono-Regular, Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 13.600000381469727px; margin-top: 0px; margin-bottom: 0px; word-wrap: normal; padding: 16px; overflow: auto; line-height: 1.45; background-color: rgb(246, 248, 250); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; word-break: normal; color: rgb(36, 41, 46);" class=""><span class="pl-k" style="box-sizing: border-box; color: rgb(215, 58, 73);">class</span> <span class="pl-en" style="box-sizing: border-box; color: rgb(111, 66, 193);">A</span> {
<span class="pl-k" style="box-sizing: border-box; color: rgb(215, 58, 73);">func</span> <span class="pl-en" style="box-sizing: border-box; color: rgb(111, 66, 193);">foo</span>() {
<span class="pl-k" style="box-sizing: border-box; color: rgb(215, 58, 73);">func</span> <span class="pl-en" style="box-sizing: border-box; color: rgb(111, 66, 193);">local</span>() {
<span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 92, 197);">bar</span>()
}
methodWithEscapingClosure { [<span class="pl-k" style="box-sizing: border-box; color: rgb(215, 58, 73);">unowned</span> <span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 92, 197);">self</span>] <span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 92, 197);">_</span> <span class="pl-k" style="box-sizing: border-box; color: rgb(215, 58, 73);">in</span>
<span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 92, 197);">self</span>.<span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 92, 197);">bar</span>()
<span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 92, 197);">local</span>() <span class="pl-c" style="box-sizing: border-box; color: rgb(106, 115, 125);"><span class="pl-c" style="box-sizing: border-box;">//</span> this leaks because local captures self</span>
<span class="pl-c" style="box-sizing: border-box; color: rgb(106, 115, 125);"></span> }
}
<span class="pl-k" style="box-sizing: border-box; color: rgb(215, 58, 73);">func</span> <span class="pl-en" style="box-sizing: border-box; color: rgb(111, 66, 193);">bar</span>() {
}
}</pre><div class=""><br class=""></div></div><div class="">Its sneaky because local’s capturing of self is not obvious if you’ve trained your brain to watch out for calls prefixed with self. I would suggest having the compiler force users to make self capturing explicit, the same way it does for closures:</div><div class=""><br class=""></div><div class=""><pre style="box-sizing: border-box; font-family: SFMono-Regular, Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 13.600000381469727px; margin-top: 0px; margin-bottom: 0px; word-wrap: normal; padding: 16px; overflow: auto; line-height: 1.45; background-color: rgb(246, 248, 250); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; word-break: normal; color: rgb(36, 41, 46);" class=""><span class="pl-k" style="box-sizing: border-box; color: rgb(215, 58, 73);">class</span> <span class="pl-en" style="box-sizing: border-box; color: rgb(111, 66, 193);">A</span> {
<span class="pl-k" style="box-sizing: border-box; color: rgb(215, 58, 73);">func</span> <span class="pl-en" style="box-sizing: border-box; color: rgb(111, 66, 193);">foo</span>() {
<span class="pl-k" style="box-sizing: border-box; color: rgb(215, 58, 73);">func</span> <span class="pl-en" style="box-sizing: border-box; color: rgb(111, 66, 193);">local</span>() {
<span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 92, 197);">bar</span>() // error: Call to method ‘bar' in function ‘local' requires explicit 'self.' to make capture semantics explicit
}
<span class="Apple-tab-span" style="white-space:pre">        </span>// ...
}
}</pre><div class=""><br class=""></div></div><div class="">What do you think?</div><div class=""><br class=""></div><div class="">David.</div><div class=""><br class=""></div></body></html>