# Removing events from MidiBuffer while iterating

**URL:** <https://forum.juce.com/t/removing-events-from-midibuffer-while-iterating/8298>\
**Category:** General JUCE discussion\
**Created:** [March 9, 2012, 11:20am UTC](https://forum.juce.com/t/removing-events-from-midibuffer-while-iterating/8298 "2012-03-09T11:20:47Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![atom](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/atom/32/113_2.png) [@atom](https://forum.juce.com/u/atom)\
**Post date:** [March 9, 2012, 11:20am UTC](https://forum.juce.com/t/removing-events-from-midibuffer-while-iterating/8298/1 "2012-03-09T11:20:47Z")

</div>

When i use the MidiBuffer::Iterator can i clear messages in the iteration loop or will it break the iterator and i need to re-iterate the buffer. I want to clear some events based on the MidiThru option while processing in the processBlock() AudioProcessor call.

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [March 9, 2012, 12:12pm UTC](https://forum.juce.com/t/removing-events-from-midibuffer-while-iterating/8298/2 "2012-03-09T12:12:18Z")

</div>

That’d definitely break, it’s a packed structure so the iterator would have to start again from the beginning.

---

<div class="post-metadata">

**Author:** ![atom](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/atom/32/113_2.png) [@atom](https://forum.juce.com/u/atom)\
**Post date:** [March 9, 2012, 2:49pm UTC](https://forum.juce.com/t/removing-events-from-midibuffer-while-iterating/8298/3 "2012-03-09T14:49:43Z")

</div>

Anyone has any suggestions on how to remove selected (based on the midi channel) MidiMessages from a MidiBuffer without re-iterating it and without additional memory allocations?

btw. gr8 avatar Jules, you look like a move star or someone of equal importance.

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [March 9, 2012, 3:12pm UTC](https://forum.juce.com/t/removing-events-from-midibuffer-while-iterating/8298/4 "2012-03-09T15:12:55Z")

</div>

Depending on what you’re doing, the best approach is probably to have a second buffer, and copy any events into it that you DO want while iterating, and then swap them over afterwards (which is very quick to do).

> [@](#):
>
> btw. gr8 avatar Jules, you look like a move star or someone of equal importance.

Well, my next-door neighbour is one of the UK’s top portrait photographers, and she owed me a favour… 😉

---

<div class="post-metadata">

**Author:** ![atom](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/atom/32/113_2.png) [@atom](https://forum.juce.com/u/atom)\
**Post date:** [March 9, 2012, 3:15pm UTC](https://forum.juce.com/t/removing-events-from-midibuffer-while-iterating/8298/5 "2012-03-09T15:15:20Z")

</div>

I thought about keeping a second MidiBuffer with some pre-allocated data and copy the wanted messages to it and then swap back as you say. I’ll try that.

---

<div class="post-metadata">

**Author:** ![SwingCoder](https://avatars.discourse-cdn.com/v4/letter/s/cab0a1/32.png) [@SwingCoder](https://forum.juce.com/u/SwingCoder)\
**Post date:** [March 9, 2012, 3:29pm UTC](https://forum.juce.com/t/removing-events-from-midibuffer-while-iterating/8298/6 "2012-03-09T15:29:47Z")

</div>

> [@atom](#):
>
> btw. gr8 avatar Jules, you look like a move star or someone of equal importance.

Agree!

---

<div class="post-metadata">

**Author:** ![Bruce\_Wheaton](https://avatars.discourse-cdn.com/v4/letter/b/71c47a/32.png) [@Bruce\_Wheaton](https://forum.juce.com/u/Bruce_Wheaton)\
**Post date:** [March 9, 2012, 5:05pm UTC](https://forum.juce.com/t/removing-events-from-midibuffer-while-iterating/8298/7 "2012-03-09T17:05:31Z")

</div>

> [@SwingCoder](#):
>
> [quote=“atom”]btw. gr8 avatar Jules, you look like a move star or someone of equal importance.

Agree![/quote]

Yeah, weird. I’m getting some cognitive dissonance between movie star and programming genius.
