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?