C-to-Swift function calls, are they glued? Specifically, CGEvent.copy()?

It doesn't for me:

let c = copyEvent(event)
print(myRetainCount(unsafeBitCast(c, to: Int.self))) // 1

just don't do that to query retain count:

print(CFGetRetainCount(event)) // 2

ARC is hiding retain count logic from us so (among other things) it is free to do some fancy optimisations with those.


Were you able to fix the original problem?

Is this what you have?

    let newEvent = ... // +1 here, e.g. a copy
    return Unmanaged.passUnretained(newEvent)

which is equivalent to:

    let newEvent = ... // +1 here, e.g. a copy
    let result = Unmanaged.passUnretained(newEvent)
    // newEvent could be gone at this point as nothing holding it anymore
    return result // crash boom bang

that's the problem then. When creating a new event - return it at +1:

    let newEvent = ... // +1 here, e.g. a copy
    // nobody is holding newEvent, but us (via "newEvent" binding)
    return Unmanaged.passRetained(newEvent)
    // temporary +2, by the time of return +1 as it should be
    // (newEvent binding is going away so +2 will change to +1 as we return)

and when returning the original event:

    // the caller is holding originalEvent at some positive X
    // let's not change it
    return Unmanaged.passUnretained(originalEvent)