
使用 Valgrind Memcheckmemcheck工具的使用方式如下:valgrind --tool=memcheck ./a.out从上面的命令可以清楚的看到, 主要的命令是valgrind,而我们想使用的工具是通过'-tool'选项来指定的. 上面的‘a.out’指的是我们想使用memcheck运行的可执行文件.该工具可以检测下列与
内存相关的问题 :未释放内存的使用对释放后内存的读/写对已
分配内存块尾部的读/写内存泄露不匹配的使用malloc/new/new[] 和 free/delete/delete[]重复释放内存注意: 上面列出的并不很全面,但却包含了能被该工具检测到的很多普遍的问题.
让我们一个一个地对上面的场景进行讨论:注意: 下面讨论的所有测试代码都应该使用gcc并且加上-g选项(用来在memcheck的输出中生成行号)进行编译. 就想我们之前讨论过的 C程序被编译成可执行文件, 它需要经历四个不同的阶段. ToB蓝波湾翻译于 1 年 前0人顶顶 翻译的不错哦!1. 使用未初始化的内存Code :#include <stdio.h>#include <stdlib.h>int main(void){char *pchar c = *pprintf("\n [%c]\n",c)return 0}在上面的代码中,我们尝试使用未初始化的指针 ‘p’.让我们运行Memcheck来看下结果.$ valgrind --tool=memcheck ./val==2862== Memcheck, a memory error detector==2862== Copyright (C) 2002-2009, and GNU GPL'd, by Julian Seward et al.==2862== Using Valgrind-3.6.0.SVN-Debian and LibVEXrerun with -h for copyright info==2862== Command: ./val==2862====2862== Use of uninitialised value of size 8==2862==at 0x400530: main (valgrind.c:8)==2862==[#]==2862====2862== HEAP SUMMARY:==2862== in use at exit: 0 bytes in 0 blocks==2862== total heap usage: 0 allocs, 0 frees, 0 bytes allocated==2862====2862== All heap blocks were freed -- no leaks are possible==2862====2862== For counts of detected and suppressed errors, rerun with: -v==2862== Use --track-origins=yes to see where uninitialized values come from==2862== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 4 from 4)从上面的输出可以看到,Valgrind检测到了未初始化的变量,然后给出了警告(上面加粗的几行(译者注:貌似上面没有加粗的)).2. 在内存被释放后进行读/写Code :#include <stdio.h>#include <stdlib.h>int main(void){char *p = malloc(1)*p = 'a'char c = *pprintf("\n [%c]\n",c)free(p)c = *preturn 0}上面的代码中,我们有一个释放了内存的指针 ‘p’ 然后我们又尝试利用指针获取值.让我们运行memcheck来看一下Valgrind对这种情况是如何反应的.$ valgrind --tool=memcheck ./val==2849== Memcheck, a memory error detector==2849== Copyright (C) 2002-2009, and GNU GPL'd, by Julian Seward et al.==2849== Using Valgrind-3.6.0.SVN-Debian and LibVEXrerun with -h for copyright info==2849== Command: ./val==2849== [a]==2849== Invalid read of size 1==2849==at 0x400603: main (valgrind.c:30)==2849== Address 0x51b0040 is 0 bytes inside a block of size 1 free'd==2849==at 0x4C270BD: free (vg_replace_malloc.c:366)==2849==by 0x4005FE: main (valgrind.c:29)==2849====2849====2849== HEAP SUMMARY:==2849== in use at exit: 0 bytes in 0 blocks==2849== total heap usage: 1 allocs, 1 frees, 1 bytes allocated==2849====2849== All heap blocks were freed -- no leaks are possible==2849====2849== For counts of detected and suppressed errors, rerun with: -v==2849== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 4 from 4)从上面的输出内容可以看到,Valgrind检测到了无效的读取 *** 作然后输出了警告 ‘Invalid read of size 1′.另注,使用gdb来调试c程序.3. 从已分配内存块的尾部进行读/写Code :#include <stdio.h>#include <stdlib.h>int main(void){char *p = malloc(1)*p = 'a'char c = *(p+1)printf("\n [%c]\n",c)free(p)return 0}基于glib的程序,由于内存分配与回收机制的问题,valgrind会做出错误的统计。Glib提供了针对valgrind友好的内存分配手段,使用方式如下:
G_SLICE=always-malloc G_DEBUG=gc-friendly valgrind--tool=memcheck --leak-check=full --leak-resolution=high --num-callers=20--suppressions=<filename>--gen-suppressions=all --log-file=vgdumpgdbus_test
参数说明:
G_SLICE=always-malloc与G_DEBUG=gc-friendly,通知glib使用valgrind友好的内存分配手段
--tool=memcheck --leak-check=full --leak-resolution=high--num-callers=20,valgrind参数
--suppressions,根据filename的内容,压制不必要的内存泄露提示,如,g_type_init里会分配一些运行期内不会释放的空间,并不能认为是内存泄露,压制的语法可以参考附件。
--log-file,可以将log打入文件中,用于后续分析
Gdbus_test,要测试的程序。
评论列表(0条)