[swift-users] Function to unsafe pointer and back

Lou Zell lzell11 at gmail.com
Wed Aug 31 19:41:13 CDT 2016


Hi all,
In both examples below I'm attempting to call a function from the
function's address.   The first example works but the second doesn't.  I
suspect I'm missing something fundamental about function invocations.  Can
someone explain why the second example fails?  First:

func doNothing() {}
func call(_ ptr: UnsafePointer<() -> ()>) {
    ptr.pointee()
}
var x = doNothing
call(&x)


There's no crash there.  Everything works as expected - in that it does
nothing.  So far so good, but this crashes:

doNothing
$R16: () -> () = 0x0000000100561060 $__lldb_expr2`__lldb_expr_1.doNothing
() -> () at repl.swift:1
let ptr = UnsafePointer<() -> ()>(bitPattern: 0x0000000100561060)
ptr!.pointee()

Crashes!  Also, I notice that the address of the pointee changes each time
I ask for it:

ptr!.pointee
$R17: () -> () = 0x0000000100567440 $__lldb_expr50`partial apply forwarder
for reabstraction thunk helper from @callee_owned (@in ()) -> (@out ()) to
@callee_owned () -> () at repl49.swift
ptr!.pointee
$R18: () -> () = 0x0000000100567740 $__lldb_expr52`partial apply forwarder
for reabstraction thunk helper from @callee_owned (@in ()) -> (@out ()) to
@callee_owned () -> () at repl51.swift
Questions:
1. Why does the first example succeed but the second one fail?
2. Why does the pointee address change on sequential calls?
3. What is a reabstraction thunk helper (do I want to know :))

I know it's a lot to ask, if there's something you would rather point me to
to read that's a-ok!

Thanks,
Lou
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20160831/59a11c66/attachment.html>


More information about the swift-users mailing list