petegoodliffe / PGMidi

PGMidi iOS MIDI library and example code
296 stars 83 forks source link

How to stop session when I'm done with it? #3

Closed arlomedia closed 12 years ago

arlomedia commented 12 years ago

I only need to use PGMidi in one part of my app, so I'd like to shut it down when I'm done using it.

I tweaked the enableNetwork method so that session.enabled is set to enabled, instead of YES, so I can call enableNetwork:NO. I also changed the connectionPolicy to MIDINetworkConnectionPolicy_NoOne when enabled=NO. But after I call enableNetwork:NO, the app still receives incoming MIDI messages. If I've released the PGMidi object, this causes a crash.

What else should I do when I'm finished using PGMidi?

arlomedia commented 12 years ago

Okay, the problem is that I was calling enableNetwork:NO and releasing the PGMidi class from the viewDidUnload method in a modal view, forgetting that views aren't necessarily unloaded when they are dismissed. So this code wasn't running. Moving this code to my "done" method does shut down the class and stops any further incoming MIDI messages.

I then realized that my modification to enableNetwork disconnects MIDI for the whole device, which would interfere with other MIDI apps the user is running. So I took this out and now I'm simply releasing the PGMidi class when I'm done with it.

If I wanted to leave PGMidi running in case I open the view again, I found that I could add a check "if (delegate)" inside the midiRead method. This avoids a crash if the view from which PGMidi was started is dismissed but not unloaded and PGMidi receives incoming MIDI data in that state. I don't know what other parts of PGMidi could cause a problem in that state, that's the only problem I've seen. So to be safe, I'll just release it.