I have a Heap leak?

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!

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.

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!

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:

#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.

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!

Those are called from heap_2:


Thanks.

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.

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 :slight_smile:

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/