[Experimental] Can Swift.Unmanaged force free the object ?

Updated
Mar 19, 2020 1:45 AM
Created
Mar 18, 2020 3:52 PM
Tags
SwiftMemo
Keywords

class Object {}

let o = Object()

let p = Unmanaged.passRetained(o).toOpaque()

p.deallocate()

print(o)

the line of print will cause following

Playground execution failed:

error: Execution was interrupted, reason: EXC_BAD_ACCESS (code=EXC_I386_GPFLT).
The process has been left at the point where it was interrupted, use "thread return -x" to return to the state before expression evaluation.

* thread #1, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=EXC_I386_GPFLT)
  * frame #0: 0x000000010630f959 libswiftCore.dylib`swift_getObjectType + 57
    frame #1: 0x00000001062b8a61 libswiftCore.dylib`findDynamicValueAndType(swift::OpaqueValue*, swift::TargetMetadata<swift::InProcess> const*, swift::OpaqueValue*&, swift::TargetMetadata<swift::InProcess> const*&, bool&, bool, bool) + 321
    frame #2: 0x00000001062bb8c2 libswiftCore.dylib`_dynamicCastToExistential(swift::OpaqueValue*, swift::OpaqueValue*, swift::TargetMetadata<swift::InProcess> const*, swift::TargetExistentialTypeMetadata<swift::InProcess> const*, swift::DynamicCastFlags) + 130
    frame #3: 0x000000010626a372 libswiftCore.dylib`function signature specialization <Arg[1] = Exploded, Arg[2] = Exploded> of generic specialization <Swift._TeeStream<Swift.String, Swift._Stdout>> of Swift._print<A where A: Swift.TextOutputStream>(_: Swift.Array<Any>, separator: Swift.String, terminator: Swift.String, to: inout A) -> () + 1138
    frame #4: 0x000000010626cadd libswiftCore.dylib`merged function signature specialization <Arg[1] = Exploded, Arg[2] = Exploded> of Swift.print(_: Any..., separator: Swift.String, terminator: Swift.String) -> () + 157
    frame #5: 0x00000001061307ef libswiftCore.dylib`Swift.print(_: Any..., separator: Swift.String, terminator: Swift.String) -> () + 31
    frame #6: 0x000000010f154536 $__lldb_expr94`main at Unmanaged.xcplaygroundpage:13:1
    frame #7: 0x0000000105f39560 MyBook`linkResources + 304
    frame #8: 0x00007fff23bd429c CoreFoundation`__CFRUNLOOP_IS_CALLING_OUT_TO_A_BLOCK__ + 12
    frame #9: 0x00007fff23bd3a08 CoreFoundation`__CFRunLoopDoBlocks + 312
    frame #10: 0x00007fff23bce894 CoreFoundation`__CFRunLoopRun + 1284
    frame #11: 0x00007fff23bce066 CoreFoundation`CFRunLoopRunSpecific + 438
    frame #12: 0x00007fff384c0bb0 GraphicsServices`GSEventRunModal + 65
    frame #13: 0x00007fff48092d4d UIKitCore`UIApplicationMain + 1621
    frame #14: 0x0000000105f3962d MyBook`main + 205
    frame #15: 0x00007fff5227ec25 libdyld.dylib`start + 1