[swift-users] UnsafeMutableRawPointer to UnsafeMutablePointer<T>: EXC_BAD_ACCESS on pointee

Guillaume Lessard glessard at tffenterprises.com
Sun Sep 24 06:27:59 CDT 2017


[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




More information about the swift-users mailing list