# MidiBuffer exceptions in processBlock()

**URL:** <https://forum.juce.com/t/midibuffer-exceptions-in-processblock/32326>\
**Category:** Getting Started\
**Created:** [March 18, 2019, 10:22am UTC](https://forum.juce.com/t/midibuffer-exceptions-in-processblock/32326 "2019-03-18T10:22:20Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![duggle](https://avatars.discourse-cdn.com/v4/letter/d/ba8739/32.png) [@duggle](https://forum.juce.com/u/duggle)\
**Post date:** [March 18, 2019, 10:22am UTC](https://forum.juce.com/t/midibuffer-exceptions-in-processblock/32326/1 "2019-03-18T10:22:21Z")

</div>

Occasionally, the debugger raises exception with what seems to be zero sized message for no appearant reason.  
I have the following in processBlock() to deliver midi events produced from Juce components to the DAW Plugin output.

> ```
> for (MidiBuffer::Iterator i(midiOutDAW); i.getNextEvent(mDAW, time);)
> {
> midiMessages.addEvent(mDAW,time); //output event to DAW
> };
> midiOutDAW.clear(); 
> 
> ```

The MidiBuffer midiOutDAW is defined in PluginProcessor.h and a raw pointer MidiBuffer\* is passed to the constructor of components (MidiBuffer\* myMidiOutBufferDAW;) that generate the events

> ```
> mySlider->onValueChange = [this] {
> auto message = MidiMessage::controllerEvent(1, Get_CC(myIndex), (int)mySlider->getValue());
> myMidiOutBufferDAW->addEvent(message, 0);
> };
> 
> ```

Is the raw pointer reference threadsafe? If not, what to do?  
Thanks for input

---

<div class="post-metadata">

**Author:** ![duggle](https://avatars.discourse-cdn.com/v4/letter/d/ba8739/32.png) [@duggle](https://forum.juce.com/u/duggle)\
**Post date:** [March 19, 2019, 4:21am UTC](https://forum.juce.com/t/midibuffer-exceptions-in-processblock/32326/2 "2019-03-19T04:21:30Z")

</div>

From the class reference:  
_ **Note that altering the buffer while an iterator is using it will produce undefined behaviour.** _  
So the MidiBuffer is being iterated in the processBlock() and I am adding new data to it from components that belong to the editor. I suppose this is a problem.  
How should I be doing this?  
I thought queues were there to prevent these kinds of issues?  
Any advice greatly appreciated!
