Ubuntu下的C/C++工程经常会遇到内存泄漏、阻塞、溢出等问题。进行问题的查找和问题代码的定位很重要。
推荐 valgrind,终端开始安装:
sudo apt-get install valgrind
写好的代码生成可执行文件,用valgrind运行debug:
valgrind --tool=memcheck --leak-check=full ./Test
出来一堆调试信息,拉到最后看leak summary:
==10216==
==10216== LEAK SUMMARY:
==10216== definitely lost: 0 bytes in 0 blocks
==10216== indirectly lost: 0 bytes in 0 blocks
==10216== possibly lost: 2,492 bytes in 21 blocks
==10216== still reachable: 169,710 bytes in 1,220 blocks
==10216== of which reachable via heuristic:
==10216== newarray : 1,536 bytes in 16 blocks
==10216== suppressed: 0 bytes in 0 blocks
==10216== Reachable blocks (those to which a pointer was found) are not shown.
==10216== To see them, rerun with: --leak-check=full --show-leak-kinds=all
==10216==
==10216== For counts of detected and suppressed errors, rerun with: -v
==10216== ERROR SUMMARY: 21 errors from 21 contexts (suppressed: 0 from 0)
工程很大,possibly lost很多,但主要看前两行,definitely lost和indirectly lost,这两个如果都是0 bytes则说明工程大致上是OK的,不需要专门进行内存泄漏的细节修改;如果前两个lost不为0,则必有内存泄漏,需要好好定位查找了。 |