parameter Configuration
GCC-G: Increase debug information for valgrind precise positioning. -o0: Turn off GCC optimizations and optimize the resulting code that could cause Valgrind to be misjudged.
Valgrind
--leak-check=full
No: no memory leak detection; Summary: shows memory leaks; full: Not only shows memory leaks, but also displays error codes.
--show-reachable=yes
Details show still reachable and indirectly lost two types of memory leaks, which are not displayed by default, such as Case1 and Case4. Memory Leaks
Memory leak: Due to negligence or error, the program failed to release memory that is no longer available. --Wikipedia pointers
Start-pointer: Pointing to the memory start location
Interior-pointer: Point to middle memory location
Leakage Type
Possibly lost: The pointer points to the internal location of the memory.
Still reachable: After the program runs, it can still be accessed, although it is not released.
Definitely lost: memory cannot be accessed.
Indirectly lost: Although there is an address pointing to the space, it cannot be accessed.
examples of leaks
Pointer chain AAA Leak case BBB Leak case
------------- ------------- -------------
(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
Leak Case 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
CASE1:RRR---> BBB
void *rrr;
int main ()
{
RRR = malloc (8);
return 0;
}
==1244== LEAK SUMMARY:
==1244== still reachable:8 bytes in 1 blocks
CASE2:RRR---> AAA---> BBB
void **rrr;
int main ()
{
RRR = (void**) malloc (8);
*rrr = malloc (8);
return 0;
}
==1345== LEAK SUMMARY:
==1345== still reachable:16 bytes in 2 blocks
Case3:rrr BBB
int main ()
{
void *rrr = malloc (8);
return 0;
}
==1400== LEAK SUMMARY:
==1400== definitely lost:8 bytes in 1 blocks
Case4:rrr AAA---> BBB
int main ()
{
void **rrr = (void**) malloc (8);
*rrr = malloc (8);
return 0;
}
==1461== LEAK SUMMARY:
==1461== definitely lost:8 bytes in 1 blocks
==1461== indirectly lost:8 bytes in 1 blocks
Case5:rrr-?-> BBB
void *rrr;
int main ()
{
RRR = malloc (8);
RRR = (char*) RRR + 2;
return 0;
}
==1530== LEAK SUMMARY:
==1530== possibly Lost:8 bytes in 1 blocks
CASE6:RRR---> AAA-?-> BBB
void **rrr;
int main ()
{
RRR = (void**) malloc (8);
*rrr = malloc (8);
*rrr = (char*) (*RRR) + 2;
return 0;
}
==1587== LEAK SUMMARY:
==1587== possibly Lost:8 bytes in 1 blocks
==1587== still reachable:8 bytes in 1 blocks
Case7:rrr-?-> AAA---> BBB
void **rrr;
int main ()
{
RRR = (void**) malloc (8);
*rrr = malloc (8);
RRR = (void**) ((char*) RRR + 1);
return 0;
}
==1642== LEAK SUMMARY:
==1642== possibly Lost:16 bytes in 2 blocks
Case8:rrr-?-> AAA-?-> BBB
void **rrr;
Int main ()
{
RRR = (void**) Mall