Mbedtls_ecp_check_pubkey leaks memory on first call

I’ve written the function below to check the data of a public key received in uncompressed format (65 bytes, 0x04 + X + Y).

It works, but seems to leak memory on the first call. My free heap goes down by 92 bytes, and the memory is not recovered after the function exits. However, subsequent calls to this function don’t reduce the free heap.

If I comment out the “result = mbedtls_ecp_check_pubkey(…);” line, heap memory is not lost on the first call.

Is this expected? Am I missing a required “.…_free” call?

Here is the code. public_key_data is a pointer to 65 bytes of public key data in uncompressed format.

#define PUBLIC_KEY_DATA_SIZE 65

static int CheckPublicKey(const uint8_t *public_key_data)
{
    int result;

    mbedtls_ecp_keypair public_key;
    mbedtls_ecp_keypair_init(&public_key);

    mbedtls_ecp_group_init(&public_key.grp);
    mbedtls_ecp_group_load(&public_key.grp, MBEDTLS_ECP_DP_SECP256R1);

    mbedtls_ecp_point_init(&public_key.Q);

    result = mbedtls_ecp_point_read_binary(
        &public_key.grp,
        &public_key.Q,
        public_key_data,
        PUBLIC_KEY_DATA_SIZE
    );
    if (result != MBEDTLS_OK) goto CLEANUP;

    result = mbedtls_ecp_check_pubkey(&public_key.grp, &public_key.Q);
    if (result != MBEDTLS_OK) goto CLEANUP;

  CLEANUP:
    mbedtls_ecp_point_free(&public_key.Q);
    mbedtls_ecp_group_free(&public_key.grp);
    mbedtls_ecp_keypair_free(&public_key);

    return result;
}

I am using the ESP32 port of mbedtls, version 2.16.11 (according to version.h)