Qt(3):ubuntu下Qt Creator使用valgrind检测内存泄漏错误


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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值