ubuntu下Qt Creator使用valgrind检测内存泄漏

前端之家收集整理的这篇文章主要介绍了ubuntu下Qt Creator使用valgrind检测内存泄漏前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

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代码

  1. #include<stdlib.h>
  2. int*func(void)
  3. {
  4. int*x=malloc(10*sizeof(int));
  5. x[0]=0;//问题1:数组下标越界
  6. }
  7. intmain(void)
  8. {
  9. printf("abc\n");
  10. int*x=NULL;
  11. x=func();
  12. free(x);
  13. printf("cccc----\n");
  14. x=NULL;
  15. return0;//问题2:内存没有释放
  16. }

编译

gcc -g -o test test.c

内存检查
valgrind --tool=memcheck --leak-check=yes --show-reachable=yes ./test

结果:
  1. XXXXXXXX@Mountain:~/C_Project$valgrind--tool=memcheck--leak-check=yes--show-reachable=yes./test
  2. ==3361==Memcheck,amemoryerrordetector
  3. ==3361==Copyright(C)2002-2011,andGNUGPL'd,byJulianSewardetal.
  4. ==3361==UsingValgrind-3.7.0andLibVEX;rerunwith-hforcopyrightinfo
  5. ==3361==Command:./test
  6. ==3361==
  7. abc
  8. ==3361==Invalidwriteofsize4
  9. ==3361==at0x804842F:func(test.c:5)
  10. ==3361==by0x8048458:main(test.c:12)
  11. ==3361==Address0x41f2050is0bytesafterablockofsize40alloc'd
  12. ==3361==at0x402BE68:malloc(in/usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
  13. ==3361==by0x8048425:func(test.c:4)
  14. ==3361==by0x8048458:main(test.c:12)
  15. ==3361==
  16. cccc----
  17. ==3361==
  18. ==3361==HEAPSUMMARY:
  19. ==3361==inuseatexit:40bytesin1blocks
  20. ==3361==totalheapusage:1allocs,0frees,40bytesallocated
  21. ==3361==
  22. ==3361==40bytesin1blocksaredefinitelylostinlossrecord1of1
  23. ==3361==at0x402BE68:malloc(in/usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
  24. ==3361==by0x8048425:func(test.c:4)
  25. ==3361==by0x8048458:main(test.c:12)
  26. ==3361==
  27. ==3361==LEAKSUMMARY:
  28. ==3361==definitelylost:40bytesin1blocks
  29. ==3361==indirectlylost:0bytesin0blocks
  30. ==3361==possiblylost:0bytesin0blocks
  31. ==3361==stillreachable:0bytesin0blocks
  32. ==3361==suppressed:0bytesin0blocks
  33. ==3361==
  34. ==3361==Forcountsofdetectedandsuppressederrors,rerunwith:-v
  35. ==3361==ERRORSUMMARY:2errorsfrom2contexts(suppressed:0from0)
内存读写错误
  1. ==3361==Invalidwriteofsize4
  2. ==3361==at0x804842F:func(test.c:5)

内存泄漏:

  1. ==3361==LEAKSUMMARY:
  2. ==3361==definitelylost:40bytesin1blocks
  3. ==3361==indirectlylost:0bytesin0blocks

当将错误修改后:的输出为:

  1. XXXXXXX@Mountain:~/C_Project$valgrind--tool=memcheck--leak-check=yes--show-reachable=yes./test
  2. ==3383==Memcheck,amemoryerrordetector
  3. ==3383==Copyright(C)2002-2011,byJulianSewardetal.
  4. ==3383==UsingValgrind-3.7.0andLibVEX;rerunwith-hforcopyrightinfo
  5. ==3383==Command:./test
  6. ==3383==
  7. abc
  8. cccc----
  9. ==3383==
  10. ==3383==HEAPSUMMARY:
  11. ==3383==inuseatexit:0bytesin0blocks
  12. ==3383==totalheapusage:1allocs,1frees,40bytesallocated
  13. ==3383==
  14. ==3383==Allheapblockswerefreed--noleaksarepossible
  15. ==3383==
  16. ==3383==Forcountsofdetectedandsuppressederrors,rerunwith:-v
  17. ==3383==ERRORSUMMARY:0errorsfrom0contexts(suppressed:0from0)

很明显,已经没有错误了。


但是这里有一个问题,就是我们还无法判断内存具体泄漏的位置,待我研究后再修改此篇文章

原文链接:https://www.f2er.com/ubuntu/354588.html

猜你在找的Ubuntu相关文章