Free dynamic allocated Stack and TCB memory at task delete


I’m using FreeRTOS v10.2.1 with dynamic memory allocation for task stack and tcb. But at deletion of a task the memory doesn’t get free’d.
I see that in the function prvDeleteTCP the vPortFree is only called if configSUPPORT_DYNAMIC_ALLOCATION == 1 && configSUPPORT_STATIC_ALLOCATION == 0 && portUSING_MPU_WRAPPERS == 0.


I do not understand that portUSING_MPU_WRAPPERS must be 0 to free the memory? What is the reason for this?

Thanks and best regards

Thanks for your post - I will ask somebody in the office to investigate for you.

This table here list various combination of flags and when a free is needed:

In your specific case, tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE will be 1:

As a result, the #elif block in prvDeleteTCB should take care of freeing the memory.

                #elif( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 ) /*lint !e731 !e9029 Macro has been consolidated for readability reasons. */
			/* The task could have been allocated statically or dynamically, so
			check what was statically allocated before trying to free the
			memory. */
			if( pxTCB->ucStaticallyAllocated == tskDYNAMICALLY_ALLOCATED_STACK_AND_TCB )
				/* Both the stack and TCB were allocated dynamically, so both
				must be freed. */
				vPortFree( pxTCB->pxStack );
				vPortFree( pxTCB );
			else if( pxTCB->ucStaticallyAllocated == tskSTATICALLY_ALLOCATED_STACK_ONLY )
				/* Only the stack was statically allocated, so the TCB is the
				only memory that must be freed. */
				vPortFree( pxTCB );
				/* Neither the stack nor the TCB were allocated dynamically, so
				nothing needs to be freed. */
				configASSERT( pxTCB->ucStaticallyAllocated == tskSTATICALLY_ALLOCATED_STACK_AND_TCB	);

How are you determining that the memory is not freed?


1 Like

Ah, I’m using FreeRTOS v10.1.0 and not v10.2.1 as I stated in my first post, my fault, sorry. The difference is that tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE is defined different.

After updating to v10.2.1 it works now.

I’m using heap_4.c implementation, and I’ve watched xFreeBytesRemaining decrementing if I add and delete tasks, until I get an error from pvPortMalloc() function.