Need help restarting TLS connections with MbedTLS

Hello,
I’m working on a POST request sender program that connects to a specified url and sends a request every time you send the corresponding AT Command to the MCU. So far I’ve been able to copy the example code from ssl_client1 and paste it inside a function that runs when requested, inside it the MCU loads the certificate, initializes TLS, connects, sends the request and parses the response succesfully, however after repeating this procedure two times in a row I appear to run out of RAM, and after a fourth call my MCU crashes.

I feel like this is not the proper way to do this stuff, can someone guide me on what’s the correct procedure?
I tried separating the part of the code that involves initialization of Mbed TLS but I start getting other problems related to the ssl setup and response reading.

Thanks a lot.