0. 安装valgrind
sudo apt-get install valgrind
1. 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)
很明显,已经没有错误了。
但是这里有一个问题,就是我们还无法判断内存具体泄漏的位置,待我研究后再修改此篇文章。
参考资料:
1. http://blog.csdn.net/21aspnet/article/details/8172124
2. http://valgrind.org/docs/manual/quick-start.html#quick-start.mcrun