ofxMIDI on iOS crashes if I close other MIDI app

Hi, I’m using @danomatika’s ofxMIDI addon for my app.
Everything works fine in general, but if I open other MIDI supporting apps (such as Music IO, Mobmuplat and so on) in the background then close the app while my app is running, my app suddenly crashes.

The crash happens when it calls “void ofApp::midiInputRemoved(string name, bool isNetwork)” function. The error happens when it’s trying to close port in the function. (input->closePort)

The error message is as below.

2015-10-15 14:59:13.193 MyApp[2416:1056922] Error (Disconnecting from MIDI source): -50:Error Domain=NSMachErrorDomain Code=-50 “The operation couldn’t be completed. (Mach error -50.)”
ofxMidi: input removed: Music IO (MIDI) network: 0

2015-10-15 14:59:13.195 MyApp[2416:1056922] *** Terminating app due to uncaught exception ‘NSRangeException’, reason: ‘*** -[__NSArrayM objectAtIndex:]: index 1 beyond bounds [0 … 0]’
*** First throw call stack:
(0x184f202d8 0x1964e00e4 0x184e0385c 0x1000675e0 0x100063448 0x10006651c 0x100142f88 0x1856ec7c0 0x1856c9654 0x1856e4f94 0x184ed7a54 0x184ed79b4 0x184ed5934 0x184e012d4 0x18e4df6fc 0x1899c6fac 0x1005e4a70 0x1005e4900 0x1005c4b10 0x10047b940 0x10047b7f4 0x10047b708 0x100045194 0x196b5ea08)
libc++abi.dylib: terminating with uncaught exception of type NSException

If I delete the “closePort” part, it doesn’t crash in the beginning but if I repeat the task, it crashes at some point. I think it’s due to a memory leaking.

Could anybody help me fix this problem please? I would really appreciate.
Thank you very much!!

Are you utilizing the ofxMidiConnectionListener callback functions on iOS? They allow you to know when new midi ports are created or released in CoreAudio.

I bet your crash is coming because you open a port that was created by another app and when you close that app, you don’t received the message that it’s midi port has been closed, so that pointer is no longer valid. See the ofxMidi exampleIOS which shows how the ofxMidiConnectionListener class is used in ofApp.

Hi @danomatika
Thank you for your answer.

Do you mean adding “public ofxMidiConnectionListener” in ofApp.h file?

And “ofxMidi::setConnectionListener(this)” in the setup function of ofApp.mm file?

Yes I have all those settings in my app.
Not only that, I do have all other functions just like your ofxMIDI exampleIOS does.

It worked fine when my app is connected to external apps(desktop softwares or hardwares) through interfaces such as iRig MIDI or Mac OSX’s Network Session. It didn’t crash even when I disconnected MIDI.

I think the crash only happens with other iOS MIDI supporting apps in the background.
Any other suggestion?

Well, I tested the pdExampleIOS example with a MIDI sequencer app (Genome). No crashes.

My earlier suggestion was not about having the same functions, but about what you do in them. What are you doing in the mdiInputAdded & midiOutputAdded callbacks? Also, why are there two of each in your screenshot?

Hey @danomatika

I don’t have Genome app but I tested with some other MIDI supporting apps and I found out the crash happens depending on apps. I also tested this with your original ofxMIDI ExmpleIOS and I could get the same result.

Here’s the list of apps that I tested below.

*No crashes: Xenon, Gadget, Nanostudio, Tabletop (I could remove port without errors when closing the app)
*Crashes: Music IO, Mobmuplat (crash happens when removing the port)

The interesting thing is that the crash didn’t occur between two apps above (e.g Xenon and Music IO)
But it seems that some apps are causing my app to crash when removing MIDI port by closing the app.

Maybe some apps are using different Core MIDI protocols? I really don’t know why this happens.
I can live with the bug for now since it’s not the major issue but I would like to fix this at some point if possible.

Thank you very much and If you find a solution, please let us know.

What code do you have in the midiInputRemoved & midiOutputRemoved functions?

1 Like

It’s just like the one in ofxMIDI ExampleIOS

This is fixed in the latest commit. I was able to get the example to crash using when closing MuisicIO and trace it to an invalid index in the PGMidi sources array.

1 Like

Thank you very much @danomatika
I tested your latest commit and it works without crash now.