Valgrind can check for memory leaks, crossings, etc.
Reference:
Http://www.cnblogs.com/wangkangluo1/archive/2011/07/20/2111248.html
http://blog.csdn.net/sduliulun/article/details/7732906
To use Valgrind for memory checking:
1. Copy the debug version of the library package (including the debug versions of various base libraries, such as LIBC,LIBPTHREAD,LIBM) to the Usr/lib/debug directory. The goal is to allow Valgrind to find the symbol table for the library used by the program, Valgrind need to use the arrival of the requester function to find it based on the block tag in memory
Tar zxvf debug-mv.tar.gz-c/usr/lib
2. Copy valgrind program to/usr directory : (if the target's flash storage is insufficient, put/usr under the problem, you can put the/usr/lib/valgrind on the U disk and the 3rd step to set the environment variable accordingly. U disk re-mount to write: Mount-o remount rw/media/f728-b202/)
Tar zxvf valgrind3.tar.gz-c/usr
3. Environment variable Setting :
Vi/etc/profile:
Export path=\
......
/usr/lib/valgrind
Export Valgrind_lib=/usr/lib/valgrind
4. Under test : Valgrind test,test is a small program on target.
Note: valgrind3.tar.gz is larger, decompression will have/directory space is not enough to cause decompression failure, need to delete some/below things. or the valgrind3.tar.gz installation package on the U disk, extracted from the U disk to/usr directory, you can save the space of the compressed package
Use the Memcheck tool and file output information: valgrind-v--tool=memcheck--leak-check=full--log-file=result.txt/root/test
Where-V is the display of verbose information,--leak-check=full display detailed disclosure information,/root/test to be debugged, must be absolute path
Note that when Valgrind is not followed by--tool=***, the default is Memcheck tool. The Memcheck tool is useful for programs that are automatically exited for a period of time, but is difficult to analyze for programs that have been running in the background
6. Debug a program that has been performed in the background:
First, the background has been executing the program with Memcheck tools is difficult to analyze, need to use massif tools. Second, the program needs to be started in a valgrind way
Mode 1: Modify the SYSTEMD configuration file, replacing the program name with the script filename that executes valgrind, such as test.sh. Beware of insufficient memory to cause valgrind execution to fail, Memcheck tool will have this situation, with Massif will be better
Mode 2: Manually kill the program after the open Dbus (according to target on the application of the program, the program's normal operation depends on the things to set up, you can):
Eval export ' cat/var/tmp/dbus_session.conf '
Then execute the Valgrind startup program:
Valgrind-v--tool=massif/usr/bin/yourprogram
Because the target configuration is not high and the valgrind consumes a lot of resources (approximately 100M of RAM), it starts very long, takes nearly 1 minutes, and compares the cards. Wait and see, you can print according to Valgrind or the program itself log printing does not look like.
The execution result Massif.out.%pid is saved in the current directory. Because the generated file is a few megabytes to a few 10 trillion, target on, so you can execute in the U disk directory to let file write to U disk. Mount-o remount rw/media/f728-b202/
7. View results : The results of the Massif tool can be viewed with Massif-visualizer on Ubuntu for reference http://blog.csdn.net/tao_627/article/details/44982765
Each snapshort node on the right-hand side of the result graph shows who is allocating all the memory in the current heap. You can view the amount of usage per block, and the amount of memory used in multiple snapshort has been rising to see which memory is growing. The diagram on the left shows the growth of each chunk of memory more visually, and the area of a color represents the memory requested by a function.
8. Additional memory leak checking tips:
Sometimes even knowing where the application is not released is still difficult to find, such as using sqlite3,sqlite3 inside a memory application has not been released, this situation is actually outside the use of the sqlite3 of the location of the misuse caused. The place where the Sqlite3 is released is the Sqlite3_finalize function, and each sqlite3_prepare (or sqlite3_prepare_v2) should correspond to a sqlite3_finalize.
For such a situation:
1. Now the key function inside the counter, confirm that the call here improper disclosure, such as SQLITE3_PREPARE_V2, sqlite3_finalize count, run for a period of time to see whether the number of calls are equal. If the difference is getting bigger, it must be a problem with their call.