繁体   English   中英

为什么valgrind无法在我的“测试”程序中检测到内存泄漏?

[英]Why doesn't valgrind detect a memory leak in my “test” program?

整个测试代码包含在main.cpp中,如下所示:

#include <iostream>

using std::cout;
using std::endl;

void f(int i) {
    int* pi = new int;

    *pi = i;

    std::cout << "*pi = " << *pi << std::endl;
}

int main(int argc, char *argv[]) {
    int i = 0;

    while (i < 10000) {
        f(i);
        ++i;
    }

    return 0;
}

我编译时没有优化-O0 (来自Eclipse Qt项目),它具有:

g++ -c -pipe -O0 -Wall -W -D_REENTRANT -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -DQT_SHARED -I/usr/share/qt4/mkspecs/linux-g++ -I. -I/usr/include/qt4/QtCore -I/usr/include/qt4/QtGui -I/usr/include/qt4 -Irelease -o release/main.o main.cpp

然后链接如下:

g++ -Wl,-O0 -o test release/main.o    -L/usr/lib -lQtGui -lQtCore -lpthread 

我通过valgrind运行可执行文件,并获得以下输出:

laptop:~/workspace/test$ valgrind --leak-check=yes test
==3939== Memcheck, a memory error detector
==3939== Copyright (C) 2002-2009, and GNU GPL'd, by Julian Seward et al.
==3939== Using Valgrind-3.6.0.SVN-Debian and LibVEX; rerun with -h for copyright info
==3939== Command: test
==3939== 
==3939== 
==3939== HEAP SUMMARY:
==3939==     in use at exit: 0 bytes in 0 blocks
==3939==   total heap usage: 1,387 allocs, 1,387 frees, 64,394 bytes allocated
==3939== 
==3939== All heap blocks were freed -- no leaks are possible
==3939== 
==3939== For counts of detected and suppressed errors, rerun with: -v
==3939== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 13 from 8)

我相信valgrind应该报告内存泄漏,而不是没有泄漏的可能,因为在对new int的调用中分配了堆内存

编辑:上面的代码更改为使用std :: cout而不是qDebug(),结果相同

如果我编译并链接没有Qt依赖项的相同代码(来自Eclipse CDT项目),则valgrind将检测到泄漏:

g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"main.d" -MT"main.d" -o"main.o" "../main.cpp"

g++  -o"test2"  ./main.o   

==4604== HEAP SUMMARY:
==4604==     in use at exit: 40,000 bytes in 10,000 blocks
==4604==   total heap usage: 10,000 allocs, 0 frees, 40,000 bytes allocated
==4604== 
==4604== 40,000 bytes in 10,000 blocks are definitely lost in loss record 1 of 1
==4604==    at 0x402569A: operator new(unsigned int) (vg_replace_malloc.c:255)
==4604==    by 0x8048756: f(int) (main.cpp:7)
==4604==    by 0x80487BB: main (main.cpp:18)
==4604== 
==4604== LEAK SUMMARY:
==4604==    definitely lost: 40,000 bytes in 10,000 blocks
==4604==    indirectly lost: 0 bytes in 0 blocks
==4604==      possibly lost: 0 bytes in 0 blocks
==4604==    still reachable: 0 bytes in 0 blocks
==4604==         suppressed: 0 bytes in 0 blocks
==4604== 
==4604== For counts of detected and suppressed errors, rerun with: -v
==4604== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 19 from 8)

我正在使用32位Kubuntu 10.04,并且尝试了Debug和Release版本,我在做什么错?为什么当与Qt链接时valgrind为什么不报告内存泄漏?

发生这种情况的原因是:

valgrind --leak-check=yes test

实际上在/ usr / bin / test上运行valgrind,这是一个内置程序,用于检查文件类型并比较值,我需要做的只是:

valgrind --leak-check=yes ./test

我已经用std :: cout替换了qDebug():

==2426== HEAP SUMMARY:
==2426==     in use at exit: 40,000 bytes in 10,000 blocks
==2426==   total heap usage: 10,000 allocs, 0 frees, 40,000 bytes allocated
==2426== 
==2426== LEAK SUMMARY:
==2426==    definitely lost: 39,996 bytes in 9,999 blocks

可能是您的编译器在优化中消除了该代码,请尝试使用-O0进行编译(不使用优化选项)。

作为注释,使用带-O2的debian lenny,可以检测到泄漏:

==20547== LEAK SUMMARY:
==20547==    definitely lost: 40,000 bytes in 10,000 blocks.
==20547==      possibly lost: 0 bytes in 0 blocks.
==20547==    still reachable: 516 bytes in 7 blocks.
==20547==         suppressed: 0 bytes in 0 blocks.
==20547== Rerun with --leak-check=full to see details of leaked memory.

编译器:

    gcc version 4.3.2 (Debian 4.3.2-1.1) 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM