Page 1 of 1

mbedtls doesn't release all resources

Posted: Fri Jan 19, 2018 3:42 pm
by rwel59
I'm working on https mbedtls and can't seem to recover all resources after finishing a function.

I log starting freeheap before calling the routine then log at various points trying to understand where memory is being consumed.

The https routine is a task. Max heap consumption is about 30k in the task. After finishing the https call, I do the following:
mbedtls_ssl_session_reset(&ssl);
mbedtls_net_free(&server_fd);
vTaskDelete(NULL);

Freeheap after the task has completed is 14k lower than what it was at task start. This is just a program snippet and nothing else is running.

Re: mbedtls doesn't release all resources

Posted: Sun Jan 21, 2018 11:15 pm
by ESP_Angus
Hi rwel59,

There are some mbedTLS structures that the HTTPS example initializes once and then never frees as they're reused for each subsequent connection, but you may want to free these if you're completely done with mbedTLS.

For example, mbedtls_x509_crt_init() -> mbedtls_x509_crt_free(), mbedtls_ctr_drbg_init() -> mbedtls_ctr_drbg_free() and mbedtls_ssl_config_init() -> mbedtls_ssl_config_free(). Essentially any mbedtls_*_init has a matching mbedtls_*_free.

Also, there's a gotcha that TCP connections use a small amount of memory in the TIME_WAIT state until they expire. Although nothing like 14KB.

If these changes aren't enough to solve the problem, try the steps to find/resolve memory leaks in the heap debugging guide:
https://esp-idf.readthedocs.io/en/lates ... mory-leaks

Angus