mbedtls doesn't release all resources

rwel59
Posts: 97
Joined: Thu Oct 12, 2017 3:32 pm

mbedtls doesn't release all resources

Postby rwel59 » Fri Jan 19, 2018 3:42 pm

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.

ESP_Angus
Posts: 2344
Joined: Sun May 08, 2016 4:11 am

Re: mbedtls doesn't release all resources

Postby ESP_Angus » Sun Jan 21, 2018 11:15 pm

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

Who is online

Users browsing this forum: Google [Bot] and 112 guests