NTPClient icon indicating copy to clipboard operation
NTPClient copied to clipboard

NTPCLient.ino doesn't free the char buffer allocated inside NTPClient::getTimeDate

Open peterakroon opened this issue 7 years ago • 5 comments

The example program NTPClient repeatedly calls const char *NTPClient::getTimeDate(time_t tm). This method allocates a char buffer and returns a pointer to this buffer to the caller. NTPClient.ino should free these pointers but doesn't. The (demonstrated) result is that 24 bytes of the heap is lost on each call of getTimeDate () and the program finally crashes.

peterakroon avatar Dec 12 '17 13:12 peterakroon

Thanks for spotting this one! I've missed that probably. Adding a free(buf); will do the trick.

stelgenhof avatar Dec 13 '17 01:12 stelgenhof

Yes, that is, delete() does the trick. But of course I walked right into it.

peterakroon avatar Dec 13 '17 09:12 peterakroon

I tried to deallocate the buffer using the delete() function (as well as the free() function). Unfortunately all situations cause a system exception/crash. Not sure why that is though.

stelgenhof avatar Dec 14 '17 06:12 stelgenhof

Would you maybe show your output of the lost bytes of the heap and how you have tested it? It may help me further debugging this. Thanks!

stelgenhof avatar Dec 24 '17 01:12 stelgenhof

Please see the attached files.

NTPClient_no_delete.ino - your original example, with a line added to show heap size. NTPClient_no_delete.log - output of above program showing the heap leaking 72 bytes on each pass through the loop, finally crashing. NTPClient_with_delete.ino - as the above program, assigning the return value of getTimedate(..) to a pointer and deleting the allocated heap space after the Serial.printf. NTPClient_no_delete.log - output of above program showing the heap remaining at a constant size. Program runs forever.

Hope this helps

NTPClient with and without delete, including output.zip

peterakroon avatar Dec 24 '17 21:12 peterakroon