1.安装valgrind
sudo apt-get install valgrind
@H_403_4@2. Valgrind的使用为了使valgrind发现的错误更精确,如能够定位到源代码行,建议在编译时加上-g参数,编译优化选项请选择O0,虽然这会降低程序的执行效率。
这里用到的示例程序文件名为:test.c,选用的编译器为gcc。
生成可执行程序
gcc -g -O0 test.c -o test
生成可执行程序test之后,如何使用Valgrind来生成内存的记录文件呢?一般这样使用:
valgrind --leak-check=full --log-file=test_valgrind.log --num-callers=30 ./test
-
--num-callers 后面的60是生成的每个错误记录的追踪行数。30是随便设定的,如果没指定,默认是12行貌似(有可能有的追踪行就没显示)。
-
--leak-check=full 表示开启详细的内存泄露检测器。
ubuntu在终端检测C代码内存泄漏错误
C代码:
- #include<stdlib.h>
- int*func(void)
- {
- int*x=malloc(10*sizeof(int));
- x[0]=0;//问题1:数组下标越界
- }
- intmain(void)
- {
- printf("abc\n");
- int*x=NULL;
- x=func();
- free(x);
- printf("cccc----\n");
- x=NULL;
- return0;//问题2:内存没有释放
- }
编译
gcc -g -o test test.c
内存检查
valgrind --tool=memcheck --leak-check=yes --show-reachable=yes ./test
- XXXXXXXX@Mountain:~/C_Project$valgrind--tool=memcheck--leak-check=yes--show-reachable=yes./test
- ==3361==Memcheck,amemoryerrordetector
- ==3361==Copyright(C)2002-2011,andGNUGPL'd,byJulianSewardetal.
- ==3361==UsingValgrind-3.7.0andLibVEX;rerunwith-hforcopyrightinfo
- ==3361==Command:./test
- ==3361==
- abc
- ==3361==Invalidwriteofsize4
- ==3361==at0x804842F:func(test.c:5)
- ==3361==by0x8048458:main(test.c:12)
- ==3361==Address0x41f2050is0bytesafterablockofsize40alloc'd
- ==3361==at0x402BE68:malloc(in/usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
- ==3361==by0x8048425:func(test.c:4)
- ==3361==by0x8048458:main(test.c:12)
- ==3361==
- cccc----
- ==3361==
- ==3361==HEAPSUMMARY:
- ==3361==inuseatexit:40bytesin1blocks
- ==3361==totalheapusage:1allocs,0frees,40bytesallocated
- ==3361==
- ==3361==40bytesin1blocksaredefinitelylostinlossrecord1of1
- ==3361==at0x402BE68:malloc(in/usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
- ==3361==by0x8048425:func(test.c:4)
- ==3361==by0x8048458:main(test.c:12)
- ==3361==
- ==3361==LEAKSUMMARY:
- ==3361==definitelylost:40bytesin1blocks
- ==3361==indirectlylost:0bytesin0blocks
- ==3361==possiblylost:0bytesin0blocks
- ==3361==stillreachable:0bytesin0blocks
- ==3361==suppressed:0bytesin0blocks
- ==3361==
- ==3361==Forcountsofdetectedandsuppressederrors,rerunwith:-v
- ==3361==ERRORSUMMARY:2errorsfrom2contexts(suppressed:0from0)
- ==3361==Invalidwriteofsize4
- ==3361==at0x804842F:func(test.c:5)
内存泄漏:
- ==3361==LEAKSUMMARY:
- ==3361==definitelylost:40bytesin1blocks
- ==3361==indirectlylost:0bytesin0blocks
- XXXXXXX@Mountain:~/C_Project$valgrind--tool=memcheck--leak-check=yes--show-reachable=yes./test
- ==3383==Memcheck,amemoryerrordetector
- ==3383==Copyright(C)2002-2011,byJulianSewardetal.
- ==3383==UsingValgrind-3.7.0andLibVEX;rerunwith-hforcopyrightinfo
- ==3383==Command:./test
- ==3383==
- abc
- cccc----
- ==3383==
- ==3383==HEAPSUMMARY:
- ==3383==inuseatexit:0bytesin0blocks
- ==3383==totalheapusage:1allocs,1frees,40bytesallocated
- ==3383==
- ==3383==Allheapblockswerefreed--noleaksarepossible
- ==3383==
- ==3383==Forcountsofdetectedandsuppressederrors,rerunwith:-v
- ==3383==ERRORSUMMARY:0errorsfrom0contexts(suppressed:0from0)
很明显,已经没有错误了。