Page 1 of 1

Heap Task Tracking Issues

Posted: Mon Aug 11, 2025 3:24 pm
by themainframe
I'm trying to use Heap Task Tracking to debug some issues with memory leaks.

Calling heap_caps_print_all_task_stat_overview or heap_caps_print_all_task_stat causes a deadlock in my project because fprintf/vfprintf which is used by those functions ultimately causes heap_caps_malloc to be called, which attempts to update the task heap tracking statistics (via heap_caps_update_per_task_info_alloc), which are locked by a mutex at the start of those functions!

In the example (https://github.com/espressif/esp-idf/tr ... king/basic) this doesn't appear to happen since the fprintf calls never appear to try to allocate heap. I can't work out why the example behaves differently.

Is this expected behaviour, or am I doing something wrong here? =]

Cheers!

Re: Heap Task Tracking Issues

Posted: Tue Aug 12, 2025 11:13 am
by MicroController
Probably not expected but a bug in the IDF, see also viewtopic.php?t=46280 and https://github.com/espressif/esp-idf/issues/17232

Re: Heap Task Tracking Issues

Posted: Tue Aug 12, 2025 4:22 pm
by themainframe
I found the issue in my case.

The problem was that elsewhere in the project, bufferless access to stdout/stdin (typical of using the linenoise console on a UART I believe) was set:

Code: Select all

setvbuf(stdin, NULL, _IONBF, 0);
setvbuf(stdout, NULL, _IONBF, 0);
This makes fprintf use heap allocations. As I mentioned, during heap_caps_print_all_task_stat_overview and related functions, the heap statistics structures are locked by s_task_tracking_mutex, which means heap allocations further down the stack will result in deadlock.

Cheers!