Skip to content
This repository has been archived by the owner on Mar 27, 2018. It is now read-only.

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

Open
peterakroon opened this issue Dec 12, 2017 · 5 comments

Comments

@peterakroon
Copy link

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.

@stelgenhof
Copy link
Owner

stelgenhof commented Dec 13, 2017

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

@peterakroon
Copy link
Author

peterakroon commented Dec 13, 2017

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

@stelgenhof
Copy link
Owner

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
Copy link
Owner

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!

@peterakroon
Copy link
Author

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

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants