Memory problem when restarting MbedTLS

I am building an MQTT client on a PIC32. I can connect to my Mosquitto broker and transfer encrypted data. If I then close that session and attempt to start a new session, I get the following error: [../MbedTLS/library/ssl_tls13_generic.c] [1602][1] psa_export_public_key() returned -32512 (-0x7f00) which is MBEDTLS_ERR_SSL_ALLOC_FAILED.
When closing the session, I set net_ctx.fd = NULL (it is a SOCKET *) then call the following:
mbedtls_x509_crt_free(&(tls->ca_crt));|
mbedtls_ssl_free(&(tls->ssl_ctx));|
mbedtls_ssl_config_free(&(tls->ssl_conf));|
mbedtls_ctr_drbg_free(&(tls->ctr_drbg));|
mbedtls_entropy_free(&(tls->entropy));|

Any suggestions please?
Am I missing something in the shutdown?
Do I have to get memory_buffer_alloc.c going? I don’t have fprintf() but I can work around that.
Thanks.
Regards,
Les Grant.

Further information:
I wrote a ‘heapspace’ function using malloc/free to iteratively determine the largest free space on the heap. MQTT without TLS uses no heap. MQTT with TLS uses about 56kB of heap. After closing MQTT/TLS, I am left with only 33kB out of 64kB.
This was with V3.6.2. Maybe I should upgrade to V3.6.3
Any suggestions please?

I have updated to V3.6.3. After transferring all the settings I had changed in mbedtls_config.h, I get the following error 16 times:
In file included from ../MbedTLS/include/mbedtls/config_psa.h:21,
from ../MbedTLS/include/mbedtls/build_info.h:176,
from ../MbedTLS/library/common.h:14,
from ../MbedTLS/library/aes.c:14: <= this line changes for the different errors
../MbedTLS/include/psa/crypto_legacy.h:25:12: fatal error: stl/_prolog.h: No such file or directory

Suggestions please?

Instead of starting with the standard mbedtls_config.h and disabling features, I am going to start with a minimal config and add features.

Hello,

How you can see the Mbed TLS category of Mbed support forum is not very active, also please look at this announcement about MbedTLS move to Mbed TLS (trustedfirmware.org) .

MbedTLS is not part of Mbed project for years, so do not waste time here.

BR, Jan

Thanks for pushing me in the right direction. :slight_smile: