[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<() -> ()>) {
var x = doNothing

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

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

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

$R17: () -> () = 0x0000000100567440 $__lldb_expr50`partial apply forwarder
for reabstraction thunk helper from @callee_owned (@in ()) -> (@out ()) to
@callee_owned () -> () at repl49.swift
$R18: () -> () = 0x0000000100567740 $__lldb_expr52`partial apply forwarder
for reabstraction thunk helper from @callee_owned (@in ()) -> (@out ()) to
@callee_owned () -> () at repl51.swift
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!

-------------- 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