Memcheck: a memory error detector
• It might be a pointer to an array of C++ objects (which possess destructors) allocated with
new[]
. In this case,
some compilers store a "magic cookie" containing the array length at the start of the allocated block, and return a
pointer to just past that magic cookie, i.e. an interior-pointer. See this page for more information.
With that in mind, consider the nine possible cases described by the following figure.
Pointer chain
AAA Category
BBB Category
-------------
------------
------------
(1)
RRR ------------> BBB
DR
(2)
RRR ---> AAA ---> BBB
DR
IR
(3)
RRR
BBB
DL
(4)
RRR
AAA ---> BBB
DL
IL
(5)
RRR ------?-----> BBB
(y)DR, (n)DL
(6)
RRR ---> AAA -?-> BBB
DR
(y)IR, (n)DL
(7)
RRR -?-> AAA ---> BBB
(y)DR, (n)DL
(y)IR, (n)IL
(8)
RRR -?-> AAA -?-> BBB
(y)DR, (n)DL
(y,y)IR, (n,y)IL, (_,n)DL
(9)
RRR
AAA -?-> BBB
DL
(y)IL, (n)DL
Pointer chain legend:
- RRR: a root set node or DR block
- AAA, BBB: heap blocks
- --->: a start-pointer
- -?->: an interior-pointer
Category legend:
- DR: Directly reachable
- IR: Indirectly reachable
- DL: Directly lost
- IL: Indirectly lost
- (y)XY: it’s XY if the interior-pointer is a real pointer
- (n)XY: it’s XY if the interior-pointer is not a real pointer
- (_)XY: it’s XY in either case
Every possible case can be reduced to one of the above nine.
Memcheck merges some of these cases in its output,
resulting in the following four categories.
• "Still reachable". This covers cases 1 and 2 (for the BBB blocks) above. A start-pointer or chain of start-pointers
to the block is found. Since the block is still pointed at, the programmer could, at least in principle, have freed it
before program exit. Because these are very common and arguably not a problem, Memcheck won’t report such
blocks individually unless
--show-reachable=yes
is specified.
• "Definitely lost". This covers case 3 (for the BBB blocks) above. This means that no pointer to the block can be
found. The block is classified as "lost", because the programmer could not possibly have freed it at program exit,
since no pointer to it exists. This is likely a symptom of having lost the pointer at some earlier point in the program.
Such cases should be fixed by the programmer.
• "Indirectly lost".
This covers cases 4 and 9 (for the BBB blocks) above.
This means that the block is lost, not
because there are no pointers to it, but rather because all the blocks that point to it are themselves lost. For example,
if you have a binary tree and the root node is lost, all its children nodes will be indirectly lost. Because the problem
will disappear if the definitely lost block that caused the indirect leak is fixed, Memcheck won’t report such blocks
individually unless
--show-reachable=yes
is specified.
55