# Problems with pvPortMalloc/vPortFree

**URL:** <https://forums.freertos.org/t/problems-with-pvportmalloc-vportfree/4861>\
**Category:** Kernel\
**Created:** [October 31, 2014, 11:44am UTC](https://forums.freertos.org/t/problems-with-pvportmalloc-vportfree/4861 "2014-10-31T11:44:15Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [October 31, 2014, 11:44am UTC](https://forums.freertos.org/t/problems-with-pvportmalloc-vportfree/4861/1 "2014-10-31T11:44:15Z")

</div>

**[llapisera](https://sourceforge.net/u/llapisera/)** wrote on [Friday, October 31, 2014](https://sourceforge.net/p/freertos/discussion/382005/thread/d5ed44c1/#c940):

Hello.

I have problems when I allocate memory for using as a temporal buffer. My application  
makes something like this:

MyStruct \*p;

p = (MyStruct\*)pvPortMalloc(sizeof(MyStruct));  
if (p == NULL) {  
return SOME\_ERROR;  
}

// Use the region allocated

// finally free memory  
vPortFree((void\*)p);

If this is executed one time, there’s no problem. But sometimes when memory is freed  
the data are corrupted (data outside the heap). It seems pvPortMalloc has reserved blocks  
of memory outside the heap region. Does anyone seen this behavior?

Currently I’m working with heap\_4.c and FreeRTOS v7.3.0.

Thanks in advance.

---

<div class="post-metadata">

**Author:** ![rtel](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/rtel/32/31_2.png) [@rtel](https://forums.freertos.org/u/rtel)\
**Post date:** [October 31, 2014, 12:04pm UTC](https://forums.freertos.org/t/problems-with-pvportmalloc-vportfree/4861/2 "2014-10-31T12:04:08Z")

</div>

**[rtel](https://sourceforge.net/u/rtel/)** wrote on [Friday, October 31, 2014](https://sourceforge.net/p/freertos/discussion/382005/thread/d5ed44c1/#6383):

The most likely cause is a good old fashioned memory corruption.

When a block is allocated a structure is placed at the start of the block that notes the length of the block. The value returned by pvPortMalloc() jumps over the structure, so the application writer should never see or access it, or even know it is there.

vPortFree() uses the structure to know how to free the memory. It is likely that the information in the structure has been overwritten by something, causing the memory to be freed incorrectly.

Recent FreeRTOS versions include a configASSERT() statement to test for that occurring (as far as is possible), but as you are using an older version it might be that the assert() is not present in your code.

Regards.
