title: free heap esp32, string-formatter
body: <p>I'm using the function <strong>storePrintf</strong> for string formatting (googled it) - it works fine, but on 3d or 4th day I see the next error:</p>
  <p><code>[E][ssl_client.cpp:33] _handle_error(): [start_ssl_client():199]: (-17040) RSA - The public key operation failed : BIGNUM - Memory allocation failed</code></p>
  <p>simplified code shows the order of functions calls</p>
  <pre class="lang-cpp prettyprint-override"><code>void setup() {
    Serial.begin(115200);
  }
  char *buf ;
  char *storePrintf(const char *fmt, ...)
  {
    va_list arg;
    va_start(arg, fmt);
    size_t sz = snprintf(NULL, 0, fmt, arg);
    buf = (char *)malloc(sz + 1);
    vsprintf(buf, fmt, arg);
    va_end(arg);
    return buf;
  }
  
  void loop() {
    String s = storePrintf(&quot;==========================: ml = %.2f, val = %d\n&quot;, 0.12, 2);
     Serial.println(&quot;s = &quot; + s);
     free(buf);  
    delay(2000);
  }
  // this works well, but if code has a lot of calls to storePrintf with free(buf) gets error
  </code></pre>
  <p>I suppose that Memory allocation failed due to the never call <code>free(buf);</code> after allocation memory for <strong>buf</strong>.
  when I try to call <code>free(buf);</code> directly after calling <code>storePrintf(..)</code> in some moment appears another error:</p>
  <p><code>CORRUPT HEAP: Bad tail at 0x3ffdb993. Expected 0xbaad5678 got 0xbaad5600 assertion &quot;head != NULL&quot; failed</code></p>
  <p>Help please to fix this issue,</p>