# I have a Heap leak?

**URL:** <https://forums.freertos.org/t/i-have-a-heap-leak/9990>\
**Category:** Kernel\
**Created:** [June 8, 2020, 8:43pm UTC](https://forums.freertos.org/t/i-have-a-heap-leak/9990 "2020-06-08T20:43:42Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![roujesky](https://avatars.discourse-cdn.com/v4/letter/r/c57346/32.png) [@roujesky](https://forums.freertos.org/u/roujesky)\
**Post date:** [June 8, 2020, 8:43pm UTC](https://forums.freertos.org/t/i-have-a-heap-leak/9990/1 "2020-06-08T20:43:42Z")

</div>

Every time my task starts, I seem to loose 96 bytes off the FreeRTOS heap.  
My application does not (directly) call pvPortMalloc().  
When I create the task I call xPortGetFreeHeapSize(). In the task, right before I call vTaskDelete(NULL), I call xPortGetFreeHeapSize(). The 2 calls return values match, so I dont think it is leaking inside this task. But each time I call xPortGetFreeHeapSize() before the call to create the task, it is 96 bytes less than the previous.  
After about 90 Create and Delete task iterations, I cannot create a task because there is not enough Heap.  
This is how I create the task:

```
printf("MF starting %d\n\r", xPortGetFreeHeapSize());
if(xTaskCreate( vManualFeedingTask, ( signed char * ) "M_FEEDINGx", feedingSTACK_SIZE, NULL, uxPriority, &handleFeeder )!= pdPASS)
	printf("!!!!!!!!! could not create MANUAL FEEDER task !!!!!!!!! Free Heap %d\n\r", xPortGetFreeHeapSize());

```

I am using Heap2.c. Running Microchip PIC32795f512.  
I am struggling to figure out how to track this problem down.

Thanks!

---

<div class="post-metadata">

**Author:** ![aggarg](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/aggarg/32/4245_2.png) [@aggarg](https://forums.freertos.org/u/aggarg)\
**Post date:** [June 8, 2020, 8:55pm UTC](https://forums.freertos.org/t/i-have-a-heap-leak/9990/2 "2020-06-08T20:55:59Z")

</div>

The call to `vTaskDelete(NULL)` puts the task on the `xTasksWaitingTermination` list and the actual deletion (which frees the associated stack and TCB) happens in the idle task. Is it possible that the idle task is not getting a chance to run? You can check that by adding a small `vTaskDelay` before re-creating the task.

Thanks.

---

<div class="post-metadata">

**Author:** ![roujesky](https://avatars.discourse-cdn.com/v4/letter/r/c57346/32.png) [@roujesky](https://forums.freertos.org/u/roujesky)\
**Post date:** [June 8, 2020, 9:01pm UTC](https://forums.freertos.org/t/i-have-a-heap-leak/9990/3 "2020-06-08T21:01:03Z")

</div>

I should say that the actual application is a paper feed. When the user loads paper, my app detects that and creates the feed task. So there are probably a minimum of 5-10 seconds between creations of the feed task.

thanks!

---

<div class="post-metadata">

**Author:** ![aggarg](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/aggarg/32/4245_2.png) [@aggarg](https://forums.freertos.org/u/aggarg)\
**Post date:** [June 8, 2020, 9:28pm UTC](https://forums.freertos.org/t/i-have-a-heap-leak/9990/4 "2020-06-08T21:28:01Z")

</div>

Then we need to check where is the memory is getting leaked. Would you please define the following two macros in FreeRTOSConfig.h to see all the malloc and free calls:

```auto
#define traceMALLOC( pvAddress, uiSize ) vLogMalloc( pvAddress, uiSize )
#define traceFREE( pvAddress, uiSize ) vLogFree( pvAddress, uiSize )

```

Where the implementation of `vLogMalloc` and `vLogFree` can just simply output the `pvAddress` and `uiSize` parameters or you can examine them in debugger.

Also, how is `printf` implemented - is it allocating memory internally?

Thanks.

---

<div class="post-metadata">

**Author:** ![roujesky](https://avatars.discourse-cdn.com/v4/letter/r/c57346/32.png) [@roujesky](https://forums.freertos.org/u/roujesky)\
**Post date:** [June 8, 2020, 11:56pm UTC](https://forums.freertos.org/t/i-have-a-heap-leak/9990/5 "2020-06-08T23:56:16Z")

</div>

aggarg  
I appreciate your help.  
I added the macros, implemented vLogMalloc and vLogFree. Where do I call them? I searched and dont see anywhere that is called in freertos.  
concerning printf, I do not malloc or pvPortMalloc anywhere in my code.

thanks!

---

<div class="post-metadata">

**Author:** ![aggarg](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/aggarg/32/4245_2.png) [@aggarg](https://forums.freertos.org/u/aggarg)\
**Post date:** [June 9, 2020, 12:19am UTC](https://forums.freertos.org/t/i-have-a-heap-leak/9990/6 "2020-06-09T00:19:47Z")

</div>

Those are called from heap\_2:

> <https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/master/portable/MemMang/heap_2.c#L192>

  

> <https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/master/portable/MemMang/heap_2.c#L230>

Thanks.

---

<div class="post-metadata">

**Author:** ![richard-damon](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/richard-damon/32/38_2.png) [@richard-damon](https://forums.freertos.org/u/richard-damon)\
**Post date:** [June 9, 2020, 12:35am UTC](https://forums.freertos.org/t/i-have-a-heap-leak/9990/7 "2020-06-09T00:35:21Z")

</div>

I personally wouldn’t do this by having the task delete itself and then being recreated, but have the task continue to live and just block until it is needed again. Unless you have a real need for the memory between usages, and can be sure to get it back in time, you are just adding complexity and work.

---

<div class="post-metadata">

**Author:** ![roujesky](https://avatars.discourse-cdn.com/v4/letter/r/c57346/32.png) [@roujesky](https://forums.freertos.org/u/roujesky)\
**Post date:** [June 9, 2020, 9:30pm UTC](https://forums.freertos.org/t/i-have-a-heap-leak/9990/8 "2020-06-09T21:30:33Z")

</div>

Hi Richard:  
thanks! I contemplated doing this, but you recommending this to me is the kick in the pants I need to just do this 🙂

---

<div class="post-metadata">

**Author:** ![johankraft](https://avatars.discourse-cdn.com/v4/letter/j/e68b1a/32.png) [@johankraft](https://forums.freertos.org/u/johankraft)\
**Post date:** [June 14, 2020, 4:28pm UTC](https://forums.freertos.org/t/i-have-a-heap-leak/9990/9 "2020-06-14T16:28:34Z")

</div>

I would like to highlight our Tracealyzer tool, that can show such heap memory leaks and also identify any remaining allocations.  
See [https://percepio.com/2018/10/01/identifying-memory-leaks-through-tracing/](https://percepio.com/2018/10/01/identifying-memory-leaks-through-tracing/)
