libonion leaks 31 bytes in all programs
Closed this issue · 2 comments
I have discovered that all programs written with libonion that receive at least one request leak 31 bytes after being closed.
Steps to reproduce:
- download current version of onion
- compile examples/hello/hello.c
- run it with valgrind (I used params
--leak-check=full --show-leak-kinds=all --keep-stacktraces=alloc --track-origins=yes
) - make at least one request (access 127.0.0.1:8080 in your browser)
- stop the program with Control + C
- done, valgrind should output
still reachable: 31 bytes in 1 blocks
in its leak summary
It is a required mem leak, to prevent having to calculate the datetime string for every request, and do it only at most once every second. It is a small optimization.
I guess it could be just a global string with fixed length, and avoid the malloc. Ot have the pointer instead of globally in the onion struct.
Actually this code is a bit ugly (response.c lines 95 to 138) so feel free to submit a pull request with it in an function just for this (time string keeping), and to fix the mem leak... although the only option I see is to pre reserve it at the onion object, or maybe with a static char datetime[32]
. Maybe even we can avoid the rw lock and do an atomic pointer swap.
Aha, didn't know it was by design. Thanks for the explanation! Maybe it would be good to add this info into README as I spent some time trying to find where my program is leaking.. only to find it wasn't my code that leaked.