Debug Valgrind

来源:互联网 发布:linux mount iso 编辑:程序博客网 时间:2024/05/16 01:25

总结信息的详解:

still reachable: 指的你的指针指向的动态内存还没有被释放就退出了,一般来讲这种不会出现问题,OS会负责回收

definitely lost:   检测到内存一定泄漏了,这类错误一定要处理。

possibly lost:    说可能有泄漏,一般都是中间有二级指针分配的情况会报错。

suppressed:    统计了使用valgrind的某些参数取消了的错误。

在使用valgrind命令可以引入错误消除文件,使得对某些库产生的错误不予提示,但是这些错误最终会被统计到suppressed项目中。默认引入的错误消除文件在/usr/lib/valgrind/default.supp

Valgrind包括的工具:

Memcheck    内存检查器

Callgrind      检查程序中函数调用过程中出现的问题

Cachegrind  检查程序中缓存使用出现的问题

Helgrind       检查多线程程序中出现的竞争问题

Massif           检查程序中堆栈使用中出现的问题

Extension    利用Core提供的功能,自己编写特定的内存调试工具。

执行:

编译:gcc –g –O0 sample.c –o sample

加-g为了获得更详细的错误信息,-O0防止编译器优化影响valgrind的判断。

valgrind --tool=memcheck --leak-check=full ./sample

常用参数:

       -q, --quiet
           Run silently, and only print error messages. Useful if you are running
           regression tests or have some other automated test machinery.      

       --leak-check=<no|summary|yes|full> [default: summary]
           When enabled, search for memory leaks when the client program
           finishes. If set to summary, it says how many leaks occurred. If set
           to full or yes, it also gives details of each individual leak.

       --show-reachable=<yes|no> [default: no]
           When disabled, the memory leak detector only shows "definitely lost"
           and "possibly lost" blocks. When enabled, the leak detector also shows
           "reachable" and "indirectly lost" blocks. (In other words, it shows
           all blocks, except suppressed ones, so --show-all would be a better
           name for it.)

            "reachable" 和 "indirectly" 区别就是,直接是没有任何指针指向该内存,间接是指指向该内存的指针都位于内存泄露处。