[re-sending to the list, and elaborating]

You are making the runtime use the first word of the referent as if it were the 
reference.

The lowercase-c variable is a reference. rawPointer contains the same value, 
and points to the beginning of the object data. Your typecasting is telling the 
runtime that pointer.pointee is a *reference*, but ‘pointer’ points to object 
data. The print statement attempts to dereference data that is not a pointer at 
all.

Consider this:

let badThingToDo = rawPointer.assumingMemoryBound(to: Int.self)[2] // contains 
42

The first word at rawPointer is type data, the second has reference counts (or 
the side table pointer), the third has the first word of instance data.

Note that c is alive, and Unmanaged’s implementation is fine (we wouldn’t have 
made it this far if it weren’t).

Guillaume Lessard


_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users

Reply via email to