1.安装valgrind
sudo apt-get install valgrind
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
--log-file 后面的test_valgrind.log是指定生成的日志文件名称。
--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: 数组下标越界
- }
- int main(void)
- {
- printf("abc\n");
- int* x=NULL;
- x=func();
- free(x);
- printf("cccc----\n");
- x=NULL;
- return 0; //问题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, a memory error detector
- ==3361== Copyright (C) 2002-2011, and GNU GPL'd, by Julian Seward et al.
- ==3361== Using Valgrind-3.7.0 and LibVEX; rerun with -h for copyright info
- ==3361== Command: ./test
- ==3361==
- abc
- ==3361== Invalid write of size 4
- ==3361== at 0x804842F: func (test.c:5)
- ==3361== by 0x8048458: main (test.c:12)
- ==3361== Address 0x41f2050 is 0 bytes after a block of size 40 alloc'd
- ==3361== at 0x402BE68: malloc (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
- ==3361== by 0x8048425: func (test.c:4)
- ==3361== by 0x8048458: main (test.c:12)
- ==3361==
- cccc----
- ==3361==
- ==3361== HEAP SUMMARY:
- ==3361== in use at exit: 40 bytes in 1 blocks
- ==3361== total heap usage: 1 allocs, 0 frees, 40 bytes allocated
- ==3361==
- ==3361== 40 bytes in 1 blocks are definitely lost in loss record 1 of 1
- ==3361== at 0x402BE68: malloc (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
- ==3361== by 0x8048425: func (test.c:4)
- ==3361== by 0x8048458: main (test.c:12)
- ==3361==
- ==3361== LEAK SUMMARY:
- ==3361== definitely lost: 40 bytes in 1 blocks
- ==3361== indirectly lost: 0 bytes in 0 blocks
- ==3361== possibly lost: 0 bytes in 0 blocks
- ==3361== still reachable: 0 bytes in 0 blocks
- ==3361== suppressed: 0 bytes in 0 blocks
- ==3361==
- ==3361== For counts of detected and suppressed errors, rerun with: -v
- ==3361== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 0 from 0)
- ==3361== Invalid write of size 4
- ==3361== at 0x804842F: func (test.c:5)
内存泄漏:
- ==3361== LEAK SUMMARY:
- ==3361== definitely lost: 40 bytes in 1 blocks
- ==3361== indirectly lost: 0 bytes in 0 blocks
当将错误修改后:的输出为:
- XXXXXXX@Mountain:~/C_Project$ valgrind --tool=memcheck --leak-check=yes --show-reachable=yes ./test
- ==3383== Memcheck, a memory error detector
- ==3383== Copyright (C) 2002-2011, and GNU GPL'd, by Julian Seward et al.
- ==3383== Using Valgrind-3.7.0 and LibVEX; rerun with -h for copyright info
- ==3383== Command: ./test
- ==3383==
- abc
- cccc----
- ==3383==
- ==3383== HEAP SUMMARY:
- ==3383== in use at exit: 0 bytes in 0 blocks
- ==3383== total heap usage: 1 allocs, 1 frees, 40 bytes allocated
- ==3383==
- ==3383== All heap blocks were freed -- no leaks are possible
- ==3383==
- ==3383== For counts of detected and suppressed errors, rerun with: -v
- ==3383== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
很明显,已经没有错误了。
但是这里有一个问题,就是我们还无法判断内存具体泄漏的位置,待我研究后再修改此篇文章。