Introduction to Memory Detection Valgrind
Valgrind is a set of simulation-based program debugging and analysis tools running on Linux, and the author is Julian Seward, who won the Google-o ' Reilly Open Source Award,
it contains a kernel-a software-synthesized CPU, and a series of gadgets, each of which can perform a task-debugging, analysis, or testing, memory detection,
We can use its tools: Memcheck.
Valgrind Installation
Method 1. Valgrind official website: http://valgrind.org download
Method 2. Ubuntu sudo apt-get install Valgrind
Memcheck Detection Range:
Used to detect memory problems in A/C + + program, all memory read and write will be detected, and all calls to malloc ()/free ()/new/delete will be captured.
Therefore, it can detect the following problems:
1) The use of uninitialized memory;
2) Read/write the memory block after release;
3) Read/write beyond the allocated dynamic memory range such as malloc;
4) Read/write inappropriate memory blocks in the stack;
5) memory leaks, pointers to a piece of memory are lost forever;
6) Incorrect malloc/free or new/delete matching;
7) The DST and SRC pointer overlap issues in the memcpy () correlation function.
Memcheck Inspection Steps and Precautions
1) When compiling the program, open debug mode (the GCC compiler's-g option) to display line numbers, compile-time to remove-o1-o2 and other optimization options; is a C + + program, consider adding the option:-fno-inline, so that its function call chain will be very clear
2) Execution: Valgrind--tool=memcheck--leak-check=full--log-file=./log.txt./yourprogram
3) End of program run, view the results in Log.txt.
Results analysis
Valgrind (Memcheck) contains these 7 types of errors, which are general error hints:
1) Illegal read/illegal write errors illegal read/illegal write error
2) Use of uninitialised values using uninitialized regions
3) Use of uninitialised or unaddressable the values in system calls call when an uninitialized or non-addressable address is used
4) Illegal release of illegal frees
5) When a heap block was freed with an inappropriate deallocation function allocation and deallocation functions do not match
6) Overlapping source and destination blocks sources and destination memory blocks overlap
7) Memory leak detection leak detection
7.1 still reachable
The memory pointer still has the opportunity to use or release, and the dynamic memory pointed to by the pointer has not been released and exited.
7.2 Definitely lost
Identified memory leaks that have not been able to access this block of memory
7.3 Indirectly lost
Pointers to this memory are located at the memory leak
7.4 possibly lost
Possible memory leaks, there is still a pointer that is able to access a block of memory, but the pointer is no longer within that
Save First position
7.5 Suppressed errors generated by some libraries are not prompted, these errors are counted to the suppressed project
C + + Memory Detection Tool Valgrind