NTPClient
NTPClient copied to clipboard
NTPCLient.ino doesn't free the char buffer allocated inside NTPClient::getTimeDate
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.
Thanks for spotting this one! I've missed that probably. Adding a free(buf);
will do the trick.
Yes, that is, delete() does the trick. But of course I walked right into it.
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.
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!
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