<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 <<a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a>> 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=""> <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=""> <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=""> <br class="webkit-block-placeholder"></p><div style="font-family: Menlo; margin: 0px; font-size: 11px; line-height: normal;" class=""> <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=""> <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=""> }</div><p style="font-family: Menlo; margin: 0px; font-size: 11px; line-height: normal; min-height: 13px;" class=""> <br class="webkit-block-placeholder"></p><div style="font-family: Menlo; margin: 0px; font-size: 11px; line-height: normal;" class=""> <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=""> <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=""> </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 <memory address>, but modification requires exclusive access.</div><div style="font-family: Menlo; margin: 0px; font-size: 11px; line-height: normal;" class=""> }</div><div style="font-family: Menlo; margin: 0px; font-size: 11px; line-height: normal;" class=""> }</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=""><<a href="mailto:swift-users@swift.org" target="_blank" class="">swift-users@swift.org</a>></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 <<a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a>> wrote:<br class="">
<br class="">
> - Is there a bug in my code which is being detected in Xcode 9 ?<br class="">
<br class="">
</span>Yes. The problem here is that `doSomething(f1:)` is a mutating function, so it acts like it takes an `inout` reference to `self.helper`. That’s one mutable reference. It then calls `Car.f1()`, which tries to get a non-mutating reference to exactly the same struct. 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="">
<<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>><br class="">
<br class="">
And the general background to this in the “Ownership Manifesto"<br class="">
<br class="">
<<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>><br class="">
<span class=""><br class="">
> 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. One option is for `doSomething(f1:)` to pass the `inout` reference through to `f1`. For example:<br class="">
<br class="">
mutating func doSomething(f1: (inout Helper) -> ()) {<br class="">
f1(&self)<br class="">
}<br class="">
<br class="">
func f1(h: inout Helper) {<br class="">
_ = h.v1 // no crash<br class="">
}<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!" <<a href="http://www.apple.com/developer/" rel="noreferrer" target="_blank" class="">http://www.apple.com/<wbr class="">developer/</a>><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>