diff --git a/mm/mm_heap/mm_free.c b/mm/mm_heap/mm_free.c index 013c1551425..19a8a744a1b 100644 --- a/mm/mm_heap/mm_free.c +++ b/mm/mm_heap/mm_free.c @@ -102,7 +102,18 @@ void mm_delayfree(FAR struct mm_heap_s *heap, FAR void *mem, bool delay) nodesize = mm_malloc_size(heap, mem); #ifdef CONFIG_MM_FILL_ALLOCATIONS - memset(mem, MM_FREE_MAGIC, nodesize); +#if CONFIG_MM_FREE_DELAYCOUNT_MAX > 0 + /* If delay free is enabled, a memory node will be freed twice. + * The first time is to add the node to the delay list, and the second + * time is to actually free the node. Therefore, we only colorize the + * memory node the first time, when `delay` is set to true. + */ + + if (delay) +#endif + { + memset(mem, MM_FREE_MAGIC, nodesize); + } #endif kasan_poison(mem, nodesize); diff --git a/mm/tlsf/mm_tlsf.c b/mm/tlsf/mm_tlsf.c index f97022d4a3f..25040a7e72a 100644 --- a/mm/tlsf/mm_tlsf.c +++ b/mm/tlsf/mm_tlsf.c @@ -595,7 +595,18 @@ static void mm_delayfree(FAR struct mm_heap_s *heap, FAR void *mem, size_t size = mm_malloc_size(heap, mem); UNUSED(size); #ifdef CONFIG_MM_FILL_ALLOCATIONS - memset(mem, MM_FREE_MAGIC, size); +#if CONFIG_MM_FREE_DELAYCOUNT_MAX > 0 + /* If delay free is enabled, a memory node will be freed twice. + * The first time is to add the node to the delay list, and the second + * time is to actually free the node. Therefore, we only colorize the + * memory node the first time, when `delay` is set to true. + */ + + if (delay) +#endif + { + memset(mem, MM_FREE_MAGIC, nodesize); + } #endif kasan_poison(mem, size);