<div dir="ltr"><div>After doing some testing I looked up the implementation and <font face="monospace, monospace">Unmanaged</font> it is very misleading in how it is behaves and is implemented.</div><div><br></div><div>When you call <font face="monospace, monospace">toOpaque()</font> it returns the result of <font face="monospace, monospace">unsafeBitCast(_value, to: UnsafeRawPointer.self)</font>. <font face="monospace, monospace">_value</font><font face="arial, helvetica, sans-serif"> is an internal field we can't directly access, in your case it is the </font><font face="monospace, monospace">c</font><font face="arial, helvetica, sans-serif"> object passed in.</font></div><div><br></div><div>So it seems like it isn't returning a proper <font face="monospace, monospace">UnsafeRawPointer</font> struct.</div><div><br></div><div>If you change your <font face="monospace, monospace">let c = C()</font> into <font face="monospace, monospace">var c = C()</font> you can call</div><div><br></div><div><font face="monospace, monospace">withUnsafeMutablePointer(to: &c) { unsafeMutablePointer in</font></div><div><font face="monospace, monospace"> print(unsafeMutablePointer)</font></div><div><font face="monospace, monospace"> print(<span style="font-size:12.800000190734863px">rawPointer)</span></font></div><div><font face="monospace, monospace">}</font></div><div><br></div><div>It will print out different values for the 2 pointers even though they should be the same. If you test with <font face="monospace, monospace">withUnsafePointer(to:_:)</font> it will give the closure pointer same address as the <font face="monospace, monospace">unsafeMutablePointer</font> here.</div><div><br></div><div>If you need an UnsafeMutablePointer<C> you will need to do something like this (the pointer passed in is only considered valid for the lifetime of the closure):</div><div><br></div><div><font face="monospace, monospace">let pointer = withUnsafeMutablePointer(to: &c) {</font></div><div><font face="monospace, monospace"> return UnsafeMutablePointer<C>($0)</font></div><div><font face="monospace, monospace">}</font></div><div><br></div><div><font face="arial, helvetica, sans-serif">If you are fine to use the </font><font face="monospace, monospace">UnsafeRawPointer</font><font face="arial, helvetica, sans-serif"> from </font><font face="monospace, monospace">toOpaque()</font><font face="arial, helvetica, sans-serif">, you can convert it back into </font><font face="monospace, monospace">c</font><font face="arial, helvetica, sans-serif"> with:</font></div><div><font face="arial, helvetica, sans-serif"><br></font></div><div><font face="monospace, monospace">Unmanaged<C>.fromOpaque(rawPointer).takeUnretainedValue()</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="arial, helvetica, sans-serif">Hopefully this helped a bit.</font></div><div><font face="monospace, monospace"><br></font></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Sat, Sep 23, 2017 at 4:44 PM, Michael Ilseman 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"><span class=""><br>
> On Sep 23, 2017, at 3:44 AM, nyg nyg via swift-users <<a href="mailto:swift-users@swift.org">swift-users@swift.org</a>> wrote:<br>
><br>
> Hello all,<br>
><br>
> I'm trying to get an UnsafeMutablePointer from an<br>
> UnsafeMutableRawPointer obtained using the Unmanaged structure:<br>
><br>
> class C { var foo = 42, bar = "bar" }<br>
> let c = C()<br>
><br>
> let rawPointer = Unmanaged.passUnretained(c).<wbr>toOpaque()<br>
><br>
<br>
</span>I believe that the object “c” is effectively dead from this point onwards. Did you try putting a use of c later on to guarantee its lifetime? E.g. add a `dump(c)` at the bottom of your script?<br>
<div class="HOEnZb"><div class="h5"><br>
> let pointer = rawPointer.bindMemory(to: C.self, capacity: 1)<br>
> let pointee = pointer.pointee<br>
> print(pointee.foo) // EXC_BAD_ACCESS<br>
><br>
> Here's some LLDB output, which looks strange to me as everything seems<br>
> alright in pointer until I ask for its pointee:<br>
><br>
> (lldb) frame variable -L c<br>
> scalar: (memtest2.C) c = 0x0000000101d00030 {<br>
> 0x0000000101d00040: foo = 42<br>
> 0x0000000101d00048: bar = "bar"<br>
> }<br>
> (lldb) frame variable -L rawPointer<br>
> 0x00000001005e2e08: (UnsafeMutableRawPointer) rawPointer = {<br>
> scalar: _rawValue = 0x0000000101d00030 {<br>
> 0x0000000101d00040: foo = 42<br>
> 0x0000000101d00048: bar = "bar"<br>
> }<br>
> }<br>
> (lldb) frame variable -L pointer<br>
> 0x00000001005e2e10: (UnsafeMutablePointer<<wbr>memtest2.C>)<br>
> pointer = 0x0000000101d00030<br>
> (lldb) frame variable -L pointer._rawValue<br>
> scalar: (memtest2.C) pointer._rawValue = 0x0000000101d00030 {<br>
> 0x0000000101d00040: foo = 42<br>
> 0x0000000101d00048: bar = "bar"<br>
> }<br>
> (lldb) frame variable -L pointee<br>
> 0x00000001005e2e18: (memtest2.C) pointee = 0x00000001005b65d8 {<br>
> 0x00000001005b65e8: foo = 140736790071664<br>
> 0x00000001005b65f0: bar = ""<br>
> }<br>
><br>
> I've also tried assumingMemoryBound(to:) or simply doing:<br>
><br>
> let pointer = UnsafePointer<C>(bitPattern: Int(bitPattern: rawPointer))!<br>
> print(pointer.pointee.foo) // EXC_BAD_ACCESS<br>
><br>
> But I always get this EXC_BAD_ACCESS error. What is going on here?<br>
><br>
><br>
> Thanks for your help,<br>
> Nick<br>
> ______________________________<wbr>_________________<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/<wbr>mailman/listinfo/swift-users</a><br>
<br>
______________________________<wbr>_________________<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/<wbr>mailman/listinfo/swift-users</a><br>
</div></div></blockquote></div><br></div>