Hello,
Please make sure that you're correctly freeing all contexts. In particular, note that each call to malloc() or calloc() must have a corresponding call to free(), AND each call to mbedtls_cipher_init() must have a corresponding call to mbedtls_cipher_free(). Despite the name, mbedtls_xxx_free() functions do not free the memory used by their argument itself (and it can be on the stack or a global variable), they only free resources used by their argument.
If that's not the problem, I recommend doing static analysis or doing stress tests under a memory allocation framework. For Mbed TLS, we do static analysis with Coverity (expensive but nice), and runtime testing with AddressSanitizer and Valgrind.
Best regards,