# Memory Allocation in MidiBuffer

**URL:** <https://forum.juce.com/t/memory-allocation-in-midibuffer/8382>\
**Category:** General JUCE discussion\
**Created:** [March 29, 2012, 2:00am UTC](https://forum.juce.com/t/memory-allocation-in-midibuffer/8382 "2012-03-29T02:00:01Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![TheVinn](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/thevinn/32/806_2.png) [@TheVinn](https://forum.juce.com/u/TheVinn)\
**Post date:** [March 29, 2012, 2:00am UTC](https://forum.juce.com/t/memory-allocation-in-midibuffer/8382/1 "2012-03-29T02:00:01Z")

</div>

I have implemented a metronome using a Synthesizer. During getNextAudioBlock() I stuff a MidiMessage with noteOn at appropriate locations and then call Synthesizer::renderNextBlock().

It seems that MidiBuffer allocates memory, which takes a critical section during the audio i/o callback. Is there some what we can improve MidiMessage so it can have a pre-allocated storage or something? Or allow construction of a MidiBuffer from an existing piece of memory (similar to how AudioSampleBuffer works)? This way one can pre-allocate a sufficiently large buffer and then just keep using it.

---

<div class="post-metadata">

**Author:** ![solar3d](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/solar3d/32/135_2.png) [@solar3d](https://forum.juce.com/u/solar3d)\
**Post date:** [March 29, 2012, 6:56am UTC](https://forum.juce.com/t/memory-allocation-in-midibuffer/8382/2 "2012-03-29T06:56:27Z")

</div>

What about MidiBuffer::ensureSize()?

---

<div class="post-metadata">

**Author:** ![TheVinn](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/thevinn/32/806_2.png) [@TheVinn](https://forum.juce.com/u/TheVinn)\
**Post date:** [March 29, 2012, 2:56pm UTC](https://forum.juce.com/t/memory-allocation-in-midibuffer/8382/3 "2012-03-29T14:56:06Z")

</div>

Ahhh yeah that looks really good!

---

<div class="post-metadata">

**Author:** ![TheVinn](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/thevinn/32/806_2.png) [@TheVinn](https://forum.juce.com/u/TheVinn)\
**Post date:** [March 30, 2012, 12:28am UTC](https://forum.juce.com/t/memory-allocation-in-midibuffer/8382/4 "2012-03-30T00:28:17Z")

</div>

> [@Joerg Koehler](#):
>
> What about MidiBuffer::ensureSize()?

I’m thinking now to just make the MidiBuffer a member of my class, and after I use it in the audioIODeviceCallback() just call clear(). The same MidiBuffer will be re-used over and over again. On the first few uses, it will stretch to the the largest size it needs to be and then stay there.

---

<div class="post-metadata">

**Author:** ![TheVinn](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/thevinn/32/806_2.png) [@TheVinn](https://forum.juce.com/u/TheVinn)\
**Post date:** [March 30, 2012, 12:33am UTC](https://forum.juce.com/t/memory-allocation-in-midibuffer/8382/5 "2012-03-30T00:33:27Z")

</div>

Hmm… neither MidiBuffer nor MemoryBlock have a reset() type function that frees the memory. Which I would want to use in releaseResources() of my audio object.

---

<div class="post-metadata">

**Author:** ![dave96](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/dave96/32/8487_2.png) [@dave96](https://forum.juce.com/u/dave96)\
**Post date:** [March 30, 2012, 1:06am UTC](https://forum.juce.com/t/memory-allocation-in-midibuffer/8382/6 "2012-03-30T01:06:42Z")

</div>

When I’ve had to do this sort of thing I’ve just created a blank object on the stack and used swapWith(). This is a quick operation and when the stack object goes out of scope it will delete the old memory that was allocated. Not the most elegant solution but it works.

---

<div class="post-metadata">

**Author:** ![TheVinn](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/thevinn/32/806_2.png) [@TheVinn](https://forum.juce.com/u/TheVinn)\
**Post date:** [March 30, 2012, 1:14am UTC](https://forum.juce.com/t/memory-allocation-in-midibuffer/8382/7 "2012-03-30T01:14:48Z")

</div>

> [@dave96](#):
>
> When I’ve had to do this sort of thing I’ve just created a blank object on the stack and used swapWith()

NICE Hack!!! I will use it

---

<div class="post-metadata">

**Author:** ![solar3d](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/solar3d/32/135_2.png) [@solar3d](https://forum.juce.com/u/solar3d)\
**Post date:** [March 30, 2012, 8:41am UTC](https://forum.juce.com/t/memory-allocation-in-midibuffer/8382/8 "2012-03-30T08:41:45Z")

</div>

I would just use a ScopedPointer and create and resize the MidiBuffer in the contructor. Then I would just assign 0 to the ScopedPointer  
in ReleaseResources()…
