# HG changeset patch # User Chris Cannam # Date 1409737778 -3600 # Node ID 6b5e1edd95fcda7388dd4c22f01f27ff75ab419f # Parent 12e62865fd61a6e090096616a5139461ec4d0176 Fix to iterator invalidation (from coverity scan) diff -r 12e62865fd61 -r 6b5e1edd95fc data/fileio/MIDIFileReader.cpp --- a/data/fileio/MIDIFileReader.cpp Wed Sep 03 10:40:37 2014 +0100 +++ b/data/fileio/MIDIFileReader.cpp Wed Sep 03 10:49:38 2014 +0100 @@ -661,7 +661,10 @@ bool noteOffFound; for (MIDITrack::iterator i = m_midiComposition[track].begin(); - i != m_midiComposition[track].end(); i++) { + i != m_midiComposition[track].end(); ) { + + MIDITrack::iterator nexti = i; + ++nexti; if ((*i)->getMessageType() == MIDI_NOTE_ON && (*i)->getVelocity() > 0) { @@ -679,6 +682,13 @@ (*i)->setDuration((*j)->getTime() - (*i)->getTime()); + if (nexti == j) { + // we're about to erase j, invalidating nexti + // as well (but as this is a map, that is the + // only iterator to be invalidated) + ++nexti; + } + delete *j; m_midiComposition[track].erase(j); @@ -696,6 +706,8 @@ (*i)->setDuration((*j)->getTime() - (*i)->getTime()); } } + + i = nexti; } return notesOnTrack;