C Memory leak, or maybe not?

My advice is DON'T use C++. Avoid it whenever possible.
Completely agree 6502 . Sorry, eternal noob, most of what you say makes solid sense, but not this time 😂
Well, I guess if you used C++ and only used smart pointers, that might not be too bad. Just don't use all the rest of the C++ gorp.
 
The question is not for embedded software. And memory leaks are not related to fragmentation.
Well, fair enough, fragmentation was probably the wrong word. Let's just say "your heap will slowly grow". I guess we haven't isolated the bug yet though. We don't know for certain where the memory leak is occurring, I was just guessing its a heap leak from the repeated free then malloc shown in the listing. Then again, no-one has answered my question yet, which is why does the hashtable need to use dynamic memory at all? But maybe the leak is happening somewhere else, it's not like we've seen all the code, so who knows.

PS I think the key is that it's a long-running process. It doesn't really matter whether it's embedded, a userland program, or whatever. If the thing stays running for 6 months and it's continually doing free then malloc as the OP showed in the listing, then you're going to see small leaks growing. If you're really, really lucky, the heap manager will continually free and then alloc exactly the same block, but you will have to be really lucky.
 
C language requires some skills and discipline. If you have them, memory leaks will occur rarely. If somebody needs smart pointers and cannot work without them, then is a good idea to choose another language - Rust, Go, C#, etc.

I know there are automated tools to catch leaks, but a simple self-made solution is to redirect `malloc` and `free` to your functions or macros, keep list of allocated blocks with corresponding `malloc` call line numbers and time and remove entries when `free` is called. Then you can print a list of blocks still allocated and find reason for leaks. Sounds complex? If somebody wants to use C, this is a little exercise.
 
Hello,


Do you have any recommendation?

You have 2 big questions
1. is it leaking memory?
2. is memory use increasing unnecessarily with time?

Leak detection for daemon processes is a bit complicated sice normally the leak detection is only performed when the test application exits.

First you need to set the GDB safe path for the monitor python commands. You'll need to change this to point in /usr/local (this example uses my local build of Valgrind)

set history save
add-auto-load-safe-path /usr/local/share/gdb/auto-load/lib/libc++.so.1-gdb.py
set auto-load safe-path /home/paulf/tools/valgrind

Run the test exe

valgrind -vgdb-error=0 ./trivialleak

It will give you instructions for connecting with vgdb like this

==39967== TO DEBUG THIS PROCESS USING GDB: start GDB like this
==39967== /path/to/gdb ./trivialleak
==39967== and then give GDB the following command
==39967== target remote | /home/paulf/tools/valgrind/libexec/valgrind/../../bin/vgdb --pid=39967
==39967== --pid is optional if only one valgrind process is running

In a separate terminal run gdb and connect to the running Valgrind instance.
(gdb) target remote | /home/paulf/valgrind/coregrind/vgdb
Remote debugging using | /home/paulf/valgrind/coregrind/vgdb
relaying data between gdb and process 39967
warning: remote target does not support file transfer, attempting to access files from local filesystem.
Reading symbols from /home/paulf/valgrind/memcheck/tests/trivialleak...
Reading symbols from /libexec/ld-elf.so.1...
(No debugging symbols found in /libexec/ld-elf.so.1)
0x0000000004014380 in ?? () from /libexec/ld-elf.so.1

Now you should be able to use gdb to c[ontinue] execution. After a while you can hit ctrl-c to stop the test exe and give control back to gdb.

Use the "mo leak_check full" to run a full leak check up to the current execution point.

The little example I'm using here just has one trivial leak.

After attaching in gdb I did this
  1. set a breakpoint in main
  2. continued
  3. ran a leak check - nothing found
  4. "next" through the "test" function that performs 1000 calls to malloc without any calls to free
  5. ran a leak check and it reported 999 definite leaks and 1 still reachable
  6. ran a leak check with reachables

(gdb) b main
Note: breakpoint 1 also set at pc 0x100006e4.
Breakpoint 2 at 0x100006e4: file trivialleak.c, line 12.
(gdb) c
Continuing.

Breakpoint 1, main () at trivialleak.c:12
12 test();
(gdb) mo leak_check full
==41254== All heap blocks were freed -- no leaks are possible
==41254==
(gdb) n
13 return 0;
(gdb) mo leak_check full
==41254== 999 (+999) bytes in 999 (+999) blocks are definitely lost in new loss record 2 of 2
==41254== at 0x4088A60: malloc (vg_replace_malloc.c:451)
==41254== by 0x1000067B: test (trivialleak.c:8)
==41254== by 0x100006E7: main (trivialleak.c:12)
==41254==
==41254== LEAK SUMMARY:
==41254== definitely lost: 999 (+999) bytes in 999 (+999) blocks
==41254== indirectly lost: 0 (+0) bytes in 0 (+0) blocks
==41254== possibly lost: 0 (+0) bytes in 0 (+0) blocks
==41254== still reachable: 1 (+1) bytes in 1 (+1) blocks
==41254== suppressed: 0 (+0) bytes in 0 (+0) blocks
==41254== Reachable blocks (those to which a pointer was found) are not shown.
==41254== To see them, add 'reachable any' args to leak_check
==41254==
(gdb) mo leak_check full reachable any
==41254== 1 bytes in 1 blocks are still reachable in loss record 1 of 2
==41254== at 0x4088A60: malloc (vg_replace_malloc.c:451)
==41254== by 0x1000067B: test (trivialleak.c:8)
==41254== by 0x100006E7: main (trivialleak.c:12)
==41254==
==41254== 999 bytes in 999 blocks are definitely lost in loss record 2 of 2
==41254== at 0x4088A60: malloc (vg_replace_malloc.c:451)
==41254== by 0x1000067B: test (trivialleak.c:8)
==41254== by 0x100006E7: main (trivialleak.c:12)
==41254==
==41254== LEAK SUMMARY:
==41254== definitely lost: 999 bytes in 999 blocks
==41254== indirectly lost: 0 bytes in 0 blocks
==41254== possibly lost: 0 bytes in 0 blocks
==41254== still reachable: 1 bytes in 1 blocks
==41254== suppressed: 0 bytes in 0 blocks
 
Back
Top