Skip to content

Commit 1486c1c

Browse files
committed
Prevent buffer issues when creating events inside a handleEvent call
1 parent d3ea39e commit 1486c1c

1 file changed

Lines changed: 11 additions & 1 deletion

File tree

Source/Processors/GenericProcessor/GenericProcessor.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -729,10 +729,15 @@ int GenericProcessor::checkForEvents(bool checkForSpikes)
729729
{
730730
if (m_currentMidiBuffer->getNumEvents() > 0)
731731
{
732+
//Since adding events to the buffer inside this loop could be dangerous, create a temporal event buffer
733+
//so any call to addEvent will operate on it;
734+
MidiBuffer temporalEventBuffer;
735+
MidiBuffer* originalEventBuffer = m_currentMidiBuffer;
736+
m_currentMidiBuffer = &temporalEventBuffer;
732737
// int m = midiMessages.getNumEvents();
733738
//std::cout << m << " events received by node " << getNodeId() << std::endl;
734739

735-
MidiBuffer::Iterator i (*m_currentMidiBuffer);
740+
MidiBuffer::Iterator i(*originalEventBuffer);
736741
MidiMessage message (0xf4);
737742

738743
int samplePosition = 0;
@@ -760,6 +765,11 @@ int GenericProcessor::checkForEvents(bool checkForSpikes)
760765
handleSpike(spikeChannelArray[index], message, samplePosition);
761766
}
762767
}
768+
//Restore the original buffer pointer and, if some new event has been added here, copy it to the original buffer
769+
m_currentMidiBuffer = originalEventBuffer;
770+
if (temporalEventBuffer.getNumEvents() > 0)
771+
m_currentMidiBuffer->addEvents(temporalEventBuffer, 0, -1, 0);
772+
763773
return 0;
764774
}
765775

0 commit comments

Comments
 (0)