|
From: Aleksander <val...@al...> - 2010-01-29 00:36:21
|
Hi all,
I am writting a small tutorial of how to understand the memory leak reports
from Valgrind, and just saw that there are a lot of differences in the
naming convention used in backtraces and the summary (using
valgrind-3.5.0-Debian).
The source code would be just:
void **rrr;
int main(void)
{
/* Allocation of AAA block, start-pointer in RRR */
rrr = malloc(sizeof(void *));
/* Allocation of BBB block, start-pointer in AAA */
*rrr = strdup("bbb");
/* oops, we lost the start-pointer to AAA */
rrr = NULL;
return 0;
}
In a memleak report like this one, which shows a Case-4 type memory leak
(following the naming in the User Manual), which is a Directly Lost in AAA
and Indirectly Lost in BBB):
------------------------
4 bytes in 1 blocks are indirectly lost in loss record 1 of 2
at 0x4024C1C: malloc (vg_replace_malloc.c:195)
by 0x40B1CDF: strdup (strdup.c:43)
by 0x80487B0: main (in /home/aleksander/valgrind-memcheck)
8 (4 direct, 4 indirect) bytes in 1 blocks are definitely lost in loss
record 2 of 2
at 0x4024C1C: malloc (vg_replace_malloc.c:195)
by 0x80487B0: main (in /home/aleksander/valgrind-memcheck)
LEAK SUMMARY:
definitely lost: 4 bytes in 1 blocks
indirectly lost: 4 bytes in 1 blocks
------------------------
We can see that there is a clear naming difference between what is shown in
the backtrace of the allocation and the text in the summary.
Backtrace shows two things:
* First, the allocation backtrace BBB leak is shown as indirectly lost. So
4 bytes "Indirectly lost"
* Second, the allocation backtrace for AAA leak is shown as definitely
lost; explaining that 4 bytes where directly lost (AAA) and another 4 bytes
indirectly lost (again BBB). It sums both leaks and says that 8 bytes are
"Definitely lost". But... why is it saying 1 block only? The 8 bytes are not
only in 1 single allocated block, they are the sum of blocks AAA and BBB.
The sentence is clearly stating 8 bytes in 1 block, while that's not true.
Summary shows:
* 4 bytes "Definitely lost" ---> But wasn't it 8 bytes definitely lost as
reported in the backtrace?
* 4 bytes "Indirectly lost"
I believe that the best way of showing the backtraces and summary would be
something like this:
------------------------
4 bytes (4 indirect) in 1 blocks are definitely lost in loss record 1 of 2
at 0x4024C1C: malloc (vg_replace_malloc.c:195)
by 0x40B1CDF: strdup (strdup.c:43)
by 0x80487B0: main (in /home/aleksander/valgrind-memcheck)
8 (4 direct, 4 indirect) bytes in 2 blocks are definitely lost in loss
record 2 of 2
at 0x4024C1C: malloc (vg_replace_malloc.c:195)
by 0x80487B0: main (in /home/aleksander/valgrind-memcheck)
LEAK SUMMARY:
definitely lost: 8 bytes in 2 blocks
directly lost: 4 bytes in 1 blocks
indirectly lost: 4 bytes in 1 blocks
------------------------
So,
* backtrace for the allocation of AAA would show that the leak is
definitely lost, but specifying that it was an indirect leak.
* backtrace for the allocation of BBB would show same as before, definitely
lost specifying 4 direct and 4 indirect, but in this case specifying 2
blocks, which are the ones making the 8 bytes shown (1 block is AAA, the
other block is BBB)
* leak summary would have the same definitely lost line, but summing all
directly and indirectly lost bytes and blocks (4+4 bytes in 1+1 blocks)
* leak summary would have a new line for directly lost leaks, showing only
those bytes and block count which are directly lost (these values would be
equivalent to the current "definitely lost" line in the leak summary)
Isn't this a better way of showing the leaks? Or is there maybe something I
didn't understand?
Cheers,
-Aleksander
|
|
From: Aleksander <val...@al...> - 2010-01-29 01:02:48
|
I believe that the best way of showing the backtraces and summary would be > something like this: > ------------------------ > 4 bytes (4 indirect) in 1 blocks are definitely lost in loss record 1 of 2 > at 0x4024C1C: malloc (vg_replace_malloc.c:195) > by 0x40B1CDF: strdup (strdup.c:43) > by 0x80487B0: main (in /home/aleksander/valgrind-memcheck) > > 8 (4 direct, 4 indirect) bytes in 2 blocks are definitely lost in loss > record 2 of 2 > at 0x4024C1C: malloc (vg_replace_malloc.c:195) > by 0x80487B0: main (in /home/aleksander/valgrind-memcheck) > > LEAK SUMMARY: > definitely lost: 8 bytes in 2 blocks > directly lost: 4 bytes in 1 blocks > indirectly lost: 4 bytes in 1 blocks > ------------------------ > > So, > * backtrace for the allocation of AAA would show that the leak is > definitely lost, but specifying that it was an indirect leak. > * backtrace for the allocation of BBB would show same as before, > definitely lost specifying 4 direct and 4 indirect, but in this case > specifying 2 blocks, which are the ones making the 8 bytes shown (1 block is > AAA, the other block is BBB) > * leak summary would have the same definitely lost line, but summing all > directly and indirectly lost bytes and blocks (4+4 bytes in 1+1 blocks) > * leak summary would have a new line for directly lost leaks, showing only > those bytes and block count which are directly lost (these values would be > equivalent to the current "definitely lost" line in the leak summary) > > Sorry, this last two backtrace comments are not ok. In the first one I mean backtrace for the allocation of BBB (the indirect one), not AAA. And same for the second one, I mean backtrace for the allocation of AAA (the direct one), not BBB. |