danomatika / ofxMidi

(maintained) Midi addon for openFrameworks
Other
262 stars 72 forks source link

segfault when clearing the messages vector #76

Closed sphaero closed 3 years ago

sphaero commented 4 years ago

Just testing the MidiIn example and added sending the messages vector through osc. Now running into segfaults when clearing the messages vector.

So receiving the messages like this

void newMidiMessage(ofxMidiMessage& msg)
    {
        midiMessages.push_back(msg);
        // remove any old messages if we have too many
        while(midiMessages.size() > maxMessages) {
            midiMessages.erase(midiMessages.begin());
        }
    }

But clearing the vector when they are send:

// clear captured midi messages
    std::vector<ofxMidiMessage>::iterator itr = midiIn.midiMessages.begin();
    while (itr != midiIn.midiMessages.end() )
    {
        itr = midiIn.midiMessages.erase(itr);
    }

(could also be midiIn.midiMessages.clear() )

However this segfaults on midiMessages.erase(midiMessages.begin()); which is puzzling unless this is happening in multiple threads?

Is the ofEvent originating from a different thread? Or any other ideas?

Rg,

Arnaud

sphaero commented 4 years ago

It does seem to be a threading issue. Adding mutex locks around the code modifying the midiMessages vector makes it stable!

danomatika commented 3 years ago

added sending the messages vector through osc.

Yes, this can happen if you start to have multiple threads involved, ie. MIDI and OSC receiver etc. I can add a note to the example about this.

danomatika commented 3 years ago

Ok, see e44c14a1fc76fe33786cb21f2fd908d0b4217605.