krevis / MIDIApps

MIDI apps for Mac OS X: MIDI Monitor and SysEx Librarian.
http://www.snoize.com/
BSD 3-Clause "New" or "Revised" License
707 stars 113 forks source link

Possible solution for `MIDIPacketNext` `.pointee` Swift crashes #86

Closed orchetect closed 3 years ago

orchetect commented 3 years ago

Disregard - issue persists and remains elusive after further testing.

krevis commented 3 years ago

Thanks for the information in any case! I wasn’t aware of @_optimize.

orchetect commented 3 years ago

The issue is brutally elusive so I'm taking a hint from your implementation to see if a C-based MIDIPacketNext wrapper and unsafe memory access can prevent crashes in some of my own code.

This has been a long-standing issue with CoreMIDI in Swift and I really wish Apple would care enough to fix it.

I've seen many other libraries dogged with this same issue because it doesn't always crop up right away.

kirsteins commented 1 year ago

@orchetect Hey! Do you still experience the MIDIPacketNext.pointee crash? It seems I managed to solve this by iterating midi packet lists with:

for packet in packetList.unsafeSequence() {
    for event in packet.pointee {
        // ...
    }
}
orchetect commented 1 year ago

Yes unsafeSequence() works well to iterate packets but it's only available on more recent Apple platforms - macOS 10.15, iOS 13.0, macCatalyst 13.0.

What I found was different memory issues and crashes would appear depending on the OS version, so I had to add some robustness to handling the pointers on older platforms. If you're curious I can link some of the solutions. But If you're comfortable dropping support for older platforms then just go with the new API.

Depending on what's happening in the custom event iterator to process each packet, there may still be residual issues.

    for event in packet.pointee // invoking custom iterator