# FATAL: FreeRTOS Malloc Error!!! in MIOS32

**URL:** <https://forum.midibox.org/t/fatal-freertos-malloc-error-in-mios32/20817>\
**Category:** MIOS programming (C)\
**Tags:** ng, mios32\
**Created:** [June 29, 2020, 3:38pm UTC](https://forum.midibox.org/t/fatal-freertos-malloc-error-in-mios32/20817 "2020-06-29T15:38:01Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![m.str](https://forum.midibox.org/letter_avatar_proxy/v4/letter/m/3d9bf3/32.png) [@m.str](https://forum.midibox.org/u/m.str)\
**Post date:** [June 29, 2020, 3:38pm UTC](https://forum.midibox.org/t/fatal-freertos-malloc-error-in-mios32/20817/1 "2020-06-29T15:38:01Z")

</div>

I keep on receiving this error (topic title) in MIOS32 after switching between sections via my ngr script which proceeds to request store SysEx dumps from a connected synth.

I have a firm belief about what probably causes the error (the error I could not resolve by research, though.. the term malloc just leads me to think, that there is some memory that is not being freed properly after use)

What I did in my c files was introducing a new kind of variable (called ^macdis) for use in SysEx stream sending. What it does is that it resolves a value from a sysEx dump (identified by syxdump\_pos:x:y) into a 6-field u8 array to be further used for sending it to a Mackie C4, which uses it to display the value on six fields of one of its displays. I herefore instanciate a u8[6] array here:

s32 MBNG\_EVENT\_SendSysExStream(mios32\_midi\_port\_t port, mbng\_event\_item\_t \*item)  
{  
&nbsp; u8 \*stream\_in = item-\>stream;  
&nbsp; u32 stream\_size = item-\>stream\_size;  
&nbsp; s16 item\_value = item-\>value;  
&nbsp; //initialize array and pointer for the 6-field Display values  
&nbsp; u8 \*macdis\_array;  
&nbsp; macdis\_array = (u8 \*)malloc(sizeof(u8)\*6);

…

And I have a feeling that I just did not fully get my head around the whole pointer thing. After repeatedly calling my ngr section 1, which requests and receives the dump and subsequently uses the ^macdis flag to print one value on the mackie (and herefore using the array, which I want to be ONE array, that is cleared after the processing of one incoming value and then used for the next) the error comes up and the whole thing crashes of course. Here is how the (relevant part of) code in mbng\_event.c goes on:

case MBNG\_EVENT\_SYSEX\_VAR\_MACDIS: {

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int i, j, rest;  
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp; u8 num = item\_value;

&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp; for(i=5;i\>=0;i–){  
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp; if(i == 0 || i == 4 || i == 5){  
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp; macdis\_array _= 0x2d;_  
_&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp; }_  
_&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp; else{_  
_&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp; if(num \> 0){_  
_&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp; rest = num%10;_  
_&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp; num /= 10;_  
_&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp; switch(rest){_  
_&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp; case 0: macdis\_array_ _= 0x30; break;_  
_&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp; case 1: macdis\_array_ _= 0x31; break;_  
_&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp; case 2: macdis\_array_ _= 0x32; break;_  
_&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp; case 3: macdis\_array_ _= 0x33; break;_  
_&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp; case 4: macdis\_array_ _= 0x34; break;_  
_&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp; case 5: macdis\_array_ _= 0x35; break;_  
_&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp; case 6: macdis\_array_ _= 0x36; break;_  
_&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp; case 7: macdis\_array_ _= 0x37; break;_  
_&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp; case 8: macdis\_array_ _= 0x38; break;_  
_&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp; case 9: macdis\_array_ _= 0x39; break;_  
_&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp; default: macdis\_array_ _= 0x2d; break;_  
_&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp; }_  
_&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp; }_  
_&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp; else{_  
_&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp; macdis\_array_ _= 0x30;_  
_&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp; }_  
_&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp; }_  
_&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp; }_  
_&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp; for(j=0;j\<6;j++){_  
_&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp; MBNG\_EVENT\_ADD\_STREAM(macdis\_array[j] & 0x7f);_  
_&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp; }_  
_&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } break;_

_&nbsp;_

_&nbsp;_

_Since I cannot resolve the error by reasearch I would be grateful for anyone, who might take a quick glance at my code. Maybe someone, who is a bit more experienced in C than I am might see the (possibly obvious) error_

---

<div class="post-metadata">

**Author:** ![Hawkeye](https://forum.midibox.org/user_avatar/forum.midibox.org/hawkeye/32/223_2.png) [@Hawkeye](https://forum.midibox.org/u/Hawkeye)\
**Post date:** [June 30, 2020, 2:00pm UTC](https://forum.midibox.org/t/fatal-freertos-malloc-error-in-mios32/20817/2 "2020-06-30T14:00:19Z")

</div>

Just a supershort comment:

for every malloc() there must be matching a free() otherwise you’ll run out of heap memory at some point in time. So, do you’d need a call

free(macdis\_array);

somewhere after you don’t need to access this array any more. Btw. if it is always 6 bytes long, you can just skip the malloc (and free!) and use a variable on the stack:

u8 macdis\_array[6];

Hope this helps - have fun and many greets!  
Peter

---

<div class="post-metadata">

**Author:** ![m.str](https://forum.midibox.org/letter_avatar_proxy/v4/letter/m/3d9bf3/32.png) [@m.str](https://forum.midibox.org/u/m.str)\
**Post date:** [June 30, 2020, 6:34pm UTC](https://forum.midibox.org/t/fatal-freertos-malloc-error-in-mios32/20817/3 "2020-06-30T18:34:09Z")

</div>

Hey Peter,

thanks for the reply. At this point, it seems solved! Interestingly enough the error still came up when I ttried the first approach

> On 29.6.2020 at 5:38 PM, m.str said:
> 
> _for(j=0;j\<6;j++){_  
> _&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp; MBNG\_EVENT\_ADD\_STREAM(macdis\_array[j] & 0x7f);_  
> _&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp; }_  
> _&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } break;_

I inserted the free() call after this for loop, so after the array values had been processed and were no further used. This is what made the most sense to me. That the error still occured worries me a bit, because that MIGHT mean, that it’s something else… On the other hand I tried your second approach by turning this

&nbsp;

> On 29.6.2020 at 5:38 PM, m.str said:
> 
> //initialize array and pointer for the 6-field Display values  
> &nbsp; u8 \*macdis\_array;  
> &nbsp; macdis\_array = (u8 \*)malloc(sizeof(u8)\*6);

to this

//initialize array and pointer for the 6-field Display values  
&nbsp; u8 macdis\_array[6];

and this is working at the moment. I hope it stays functional 🙂

Best wishes,

Micha
