<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=""><div class="">Thanks a lot Zhao, using a serial queue works.</div><div class=""><br class=""></div><div class="">So both the solutions works, thanks.</div><div class=""><br class=""></div><br class=""><div><blockquote type="cite" class=""><div class="">On 24 Jul 2017, at 5:39 PM, Zhao Xin via swift-users &lt;<a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class=""><div class="gmail_default" style="font-family:georgia,serif">You can use serial queue.</div><div class="gmail_default" style="font-family:georgia,serif"><br class=""></div><div class="gmail_default"><div style="font-family: Menlo; margin: 0px; font-size: 11px; line-height: normal;" class=""><span style="color:rgb(186,45,162)" class="">class</span> Car {</div><div style="font-family: Menlo; margin: 0px; font-size: 11px; line-height: normal;" class="">&nbsp; &nbsp; <span style="color:rgb(186,45,162)" class="">var</span> helper = <span style="color:rgb(79,129,135)" class="">Helper</span>()</div><div style="font-family: Menlo; margin: 0px; font-size: 11px; line-height: normal;" class="">&nbsp; &nbsp; <span style="color:rgb(186,45,162)" class="">lazy</span> <span style="color:rgb(186,45,162)" class="">private</span> <span style="color:rgb(186,45,162)" class="">var</span> queue = <span style="color:rgb(112,61,170)" class="">DispatchQueue</span>(label: <span style="color:rgb(209,47,27)" class="">"my queue"</span>)</div><p style="font-family: Menlo; margin: 0px; font-size: 11px; line-height: normal; min-height: 13px;" class="">&nbsp;&nbsp; &nbsp;<br class="webkit-block-placeholder"></p><div style="font-family: Menlo; margin: 0px; font-size: 11px; line-height: normal;" class="">&nbsp; &nbsp; <span style="color:rgb(186,45,162)" class="">func</span> test() {</div><div style="font-family: Menlo; margin: 0px; font-size: 11px; line-height: normal;" class="">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:rgb(79,129,135)" class="">helper</span>.<span style="color:rgb(49,89,93)" class="">doSomething</span>(f1: <span style="color:rgb(49,89,93)" class="">f1</span>)</div><div style="font-family: Menlo; margin: 0px; font-size: 11px; line-height: normal;" class="">&nbsp; &nbsp; }</div><p style="font-family: Menlo; margin: 0px; font-size: 11px; line-height: normal; min-height: 13px;" class="">&nbsp;&nbsp; &nbsp;<br class="webkit-block-placeholder"></p><div style="font-family: Menlo; margin: 0px; font-size: 11px; line-height: normal;" class="">&nbsp; &nbsp; <span style="color:rgb(186,45,162)" class="">func</span> f1() {</div><div style="font-family: Menlo; margin: 0px; font-size: 11px; line-height: normal;" class="">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:rgb(79,129,135)" class="">queue</span>.<span style="color:rgb(62,30,129)" class="">async</span> {</div><div style="font-family: Menlo; margin: 0px; font-size: 11px; line-height: normal; color: rgb(0, 132, 0);" class=""><span style="" class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span><span style="color:rgb(186,45,162)" class="">_</span><span style="" class=""> = </span><span style="color:rgb(186,45,162)" class="">self</span><span style="" class="">.</span><span style="color:rgb(79,129,135)" class="">helper</span><span style="" class="">.</span><span style="color:rgb(79,129,135)" class="">v1</span><span style="" class=""> </span>//Crash - Simultaneous accesses to &lt;memory address&gt;, but modification requires exclusive access.</div><div style="font-family: Menlo; margin: 0px; font-size: 11px; line-height: normal;" class="">&nbsp; &nbsp; &nbsp; &nbsp; }</div><div style="font-family: Menlo; margin: 0px; font-size: 11px; line-height: normal;" class="">&nbsp; &nbsp; }</div><div style="font-family: Menlo; margin: 0px; font-size: 11px; line-height: normal;" class="">}</div><div style="margin: 0px; line-height: normal;" class=""><font face="georgia, serif" class=""><br class=""></font></div><div style="margin: 0px; line-height: normal;" class=""><font face="georgia, serif" class="">Zhao Xin</font></div></div></div><div class="gmail_extra"><br class=""><div class="gmail_quote">On Mon, Jul 24, 2017 at 4:22 PM, Quinn "The Eskimo!" via swift-users <span dir="ltr" class="">&lt;<a href="mailto:swift-users@swift.org" target="_blank" class="">swift-users@swift.org</a>&gt;</span> wrote:<br class=""><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span class=""><br class="">
On 24 Jul 2017, at 07:04, somu subscribe via swift-users &lt;<a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a>&gt; wrote:<br class="">
<br class="">
&gt; - Is there a bug in my code which is being detected in Xcode 9 ?<br class="">
<br class="">
</span>Yes.&nbsp; The problem here is that `doSomething(f1:)` is a mutating function, so it acts like it takes an `inout` reference to `self.helper`.&nbsp; That’s one mutable reference.&nbsp; It then calls `Car.f1()`, which tries to get a non-mutating reference to exactly the same struct.&nbsp; This is outlawed in Swift 4 as part of the memory ownership effort.<br class="">
<br class="">
You can read more about the specific change in SE-0176 “Enforce Exclusive Access to Memory”.<br class="">
<br class="">
&lt;<a href="https://github.com/apple/swift-evolution/blob/master/proposals/0176-enforce-exclusive-access-to-memory.md" rel="noreferrer" target="_blank" class="">https://github.com/apple/<wbr class="">swift-evolution/blob/master/<wbr class="">proposals/0176-enforce-<wbr class="">exclusive-access-to-memory.md</a>&gt;<br class="">
<br class="">
And the general background to this in the “Ownership Manifesto"<br class="">
<br class="">
&lt;<a href="https://github.com/apple/swift/blob/master/docs/OwnershipManifesto.md" rel="noreferrer" target="_blank" class="">https://github.com/apple/<wbr class="">swift/blob/master/docs/<wbr class="">OwnershipManifesto.md</a>&gt;<br class="">
<span class=""><br class="">
&gt; If so could you please explain and suggest an alternate approach / fix ?<br class="">
<br class="">
</span>It’s hard to offer concrete suggestions without knowing more about your high-level goals.&nbsp; One option is for `doSomething(f1:)` to pass the `inout` reference through to `f1`.&nbsp; For example:<br class="">
<br class="">
&nbsp; &nbsp; mutating func doSomething(f1: (inout Helper) -&gt; ()) {<br class="">
&nbsp; &nbsp; &nbsp; &nbsp; f1(&amp;self)<br class="">
&nbsp; &nbsp; }<br class="">
<br class="">
&nbsp; &nbsp; func f1(h: inout Helper) {<br class="">
&nbsp; &nbsp; &nbsp; &nbsp; _ = h.v1&nbsp; // no crash<br class="">
&nbsp; &nbsp; }<br class="">
<br class="">
but whether that makes sense in your code is for you to decide.<br class="">
<br class="">
Share and Enjoy<br class="">
--<br class="">
Quinn "The Eskimo!"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;<a href="http://www.apple.com/developer/" rel="noreferrer" target="_blank" class="">http://www.apple.com/<wbr class="">developer/</a>&gt;<br class="">
Apple Developer Relations, Developer Technical Support, Core OS/Hardware<br class="">
<br class="">
<br class="">
______________________________<wbr class="">_________________<br class="">
swift-users mailing list<br class="">
<a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a><br class="">
<a href="https://lists.swift.org/mailman/listinfo/swift-users" rel="noreferrer" target="_blank" class="">https://lists.swift.org/<wbr class="">mailman/listinfo/swift-users</a><br class="">
</blockquote></div><br class=""></div>
_______________________________________________<br class="">swift-users mailing list<br class=""><a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a><br class="">https://lists.swift.org/mailman/listinfo/swift-users<br class=""></div></blockquote></div><br class=""></body></html>