Fix false positives memory leaks and update documentation?
- Dominant language
- C++
- Stars
- 1.9k
- Forks
- 280
- Avg merge
- 7m
- Merged PRs (30d)
- 4
Description
When running a memory leak check of a project using loguru, some data appears which is related to Loguru. For application striving for complete memory cleanup and/or use integration tests detecting memory leaks, it can be annoying.
The following cases appear:
```
12 bytes in 1 blocks are still reachable in loss record 1 of 2
at malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
by strdup (strdup.c:42)
by logurulib::set_thread_name(char const*) (loguru.cpp:924)
by logurulib::__init() (loguru.cpp:538)
by ...
```
When looking at the code, no memory is actually leaked as the data is destroyed once the thread is destroyed, which Valgrind does not consider.
```
552 bytes in 1 blocks are still reachable in loss record 2 of 2
at malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
by __fopen_internal (iofopen.c:65)
by fopen@@GLIBC_2.2.5 (iofopen.c:89)
by logurulib::add_file(char const*, logurulib::FileMode, int) (loguru.cpp:737)
by ...
```
This is provoked by the fact that the files are not closed when the application exists. Although Linux closes them, it would be nice to be done by the library. That's when we discovered this was indeed done in the shutdown function of loguru. However, the documentation states "You generally don't need to call this", which we consider a bit misleading.
We propose updating the documentation a bit to fix the second case and maybe explicitly free the memory of the first case in the shutdown function. If these solutions seem fine we can create a pull request. Additionally, this will prevent other people from creating issues related to memory leaking :).
Contributor guide
No contributing guide indexed for this repository
Research direction
Start in loguru.cpp at logurulib::__init() around line 538, logurulib::add_file() around line 737, and logurulib::set_thread_name() around line 924; then inspect the shutdown function and its documentation. Determine how shutdown should address the reported Valgrind cases and clarify when callers need to invoke it; done means the documentation is accurate and the proposed cleanup behavior is verified.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- cpp
- Domain
- devtools
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 35/100