Page 1 of 1

Memory leakage in IDF function

Posted: Wed Jan 29, 2020 2:26 pm
by ukrsms
During debugging of my own code I found two functions that don't deallocate all the memory they used.
Memory leaked only after the first used of the function
here are the fubctions:
memory leak 20 Bytes

Code: Select all

ledc_fade_func_install(0);	
ledc_fade_func_uninstall();
and

memory leak 84 Bytes:

Code: Select all

esp_adc_cal_characteristics_t characters;
u8 calType=esp_adc_cal_characterize(ADC_UNIT_1, ADC_ATTEN_DB_11, ADC_WIDTH_BIT_12, 1100, &characters);
u32 voltage;
esp_adc_cal_get_voltage((adc_channel_t)1, &characters, &voltage);
Please, tell me whether I did something wrong, or it is a bug in IDF, or it is a normal operation of the functions?
Tested with IDF ver.4.1

Re: Memory leakage in IDF function

Posted: Fri Feb 14, 2020 4:59 pm
by ukrsms
after recent test I found one more function stealing memory:
esp_efuse_mac_get_default()
it steals 84 bytes after first calling

Re: Memory leakage in IDF function

Posted: Sat Feb 15, 2020 10:04 am
by ESP_Sprite
Thanks for the report, however, you're probably better off creating a Github issue for this, as it will automatically get tracked into the system.

Re: Memory leakage in IDF function

Posted: Tue Feb 18, 2020 4:34 am
by ESP_Angus
ukrsms wrote:
Wed Jan 29, 2020 2:26 pm
Memory leaked only after the first used of the function
These may not be memory leaks as some types of data structures in IDF are allocated on first access ("lazy initialization") but are not deinitialized.

If you repeat the same init/deinit sequence multiple times, or call the same function multiple times, and the memory use goes up each time then this is definitely a memory leak.

If you need some tools to better analyze where allocations are happening, the heap tracing feature can help:
https://docs.espressif.com/projects/esp ... ap-tracing

Angus

Re: Memory leakage in IDF function

Posted: Thu Mar 05, 2020 10:09 am
by ukrsms
Thank you, Angus

that are "lazy initialization", actually. However, it seems it is wrong behavior of the functions reported as IDF provided us with deinitialization function, however, not all memory is freed.

Re: Memory leakage in IDF function

Posted: Fri Mar 06, 2020 4:49 pm
by ukrsms
One more "Lazy initialization" found. After executing the following sequence for the first time 204 bytes disappears

Code: Select all

	TEST_ASSERT(nvs_flash_init_partition(PARTITION_LABEL_FACTORYDATA)==0);
	nvs_handle_t handle;
	TEST_ASSERT(nvs_open_from_partition(PARTITION_LABEL_FACTORYDATA, STORE_NAMESPACE_FACTORY, (nvs_open_mode_t)NVS_READWRITE, &handle)==0);
	nvs_close(handle);
	nvs_fl
ash_deinit_partition(PARTITION_LABEL_FACTORYDATA);

Re: Memory leakage in IDF function

Posted: Wed Mar 11, 2020 12:31 am
by ESP_Angus
ukrsms wrote:
Thu Mar 05, 2020 10:09 am
that are "lazy initialization", actually. However, it seems it is wrong behavior of the functions reported as IDF provided us with deinitialization function, however, not all memory is freed.
There is sometimes global intialization associated with the first time some functions is called. ledc_fade_func_install() registers an ISR internally, which involves some global initialization for the first time an ISR is allocated. Uninstalling and re-installing the same function (or a different ISR in another driver) shouldn't allocate this memory a second time.

Are these one-off small allocations causing a problem in your app?