<div dir="ltr">The way to do this is via Unmanaged:<div><font face="monospace"> let raw = Unmanaged.passUnretained(expat).toOpaque()<br></font></div><div>gets you an UnsafeMutableRawPointer you can pass into C functions. You can turn this back into an ExpatSwift with<br></div><div><font face="monospace"> let expat = Unmanaged<ExpatSwift>.fromOpaque(raw).takeUnretainedValue()</font></div><div><div><br></div></div><div>There are also retained variants that will add or remove a reference to the object in question.</div><div>Basically, if you know that the pointer won't outlive the thing it references, you want the unretained variants. This is usually what you want when you're wrapping C APIs with callbacks, especially if callbacks happen synchronously.</div><div><br></div><div>On the other hand, if the pointer lives independently of the Whatever then you may want to</div><div> - create the pointer with passRetained(), which will keep the object it points to alive</div><div> - access the object with take<b>Un</b>retainedValue() (so you don't prematurely release your reference to it), and</div><div>- call release() on it when you're done with it.</div><div>If you forget the 3rd step then you're leaking memory, and if you miscalculate and do it twice then you crash.</div><div><br></div><div>Or if you're just passing a pointer from A to B one time, you can use passRetained() + takeRetainedValue().</div><div><br></div><div>Hope that helps</div><div>Mike</div></div><br><div class="gmail_quote"><div dir="ltr">On Fri, 30 Sep 2016 at 13:11 John Brownie via swift-users <<a href="mailto:swift-users@swift.org">swift-users@swift.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div bgcolor="#FFFFFF" text="#000000">
Still working on the expat wrapper. The thing I need to do is to pass a
pointer to self to be stored in the userData that will be passed to
callbacks, allowing me to get at the local instance. I did this in Swift
2.2 as:<br>
<br>
<span style="font-family:monospace">XML_SetUserData(parser,
unsafeBitCast(self, UnsafeMutablePointer<Void>.self))</span><br>
<br>
The migrator turned this into:<br>
<br>
<span style="font-family:monospace">XML_SetUserData(parser,
unsafeBitCast(self, to: UnsafeMutableRawPointer.self))</span><br>
<br>
but it doesn't work. I don't get an appropriate pointer back in the
callback that I can turn into what I need.<br>
<br>
>From reading the migration documentation, I don't want to use
unsafeBitCast if at all possible, but I'm not clear what the alternative
is.<br>
<br>
Also, getting the userData back is something I will need. I currently
use:<br>
<br>
<span style="font-family:monospace">let theParser =
userData!.bindMemory(to: ExpatSwift.self, capacity: 1)</span><br>
<br>
I think that's correct, but I'm not getting a valid object reference.<br>
<br>
Any insight is greatly appreciated!<br>
<div>-- <br>John Brownie<br>
In Finland on furlough from SIL Papua New Guinea<br>
</div>
</div>
_______________________________________________<br>
swift-users mailing list<br>
<a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-users" rel="noreferrer" target="_blank">https://lists.swift.org/mailman/listinfo/swift-users</a><br>
</blockquote></div>