为什么 valgrind 在我的“测试”中没有检测到内存泄漏程序?
整个测试代码包含在 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() 具有相同的结果
如果我编译并链接相同的代码(来自 Eclipse CDT 项目)而没有 Qt 依赖项,则 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)
我正在使用 Kubuntu 10.04 32 位并尝试了调试和发布版本,什么我做错了吗?或者为什么 valgrind 在与 Qt 链接时不报告内存泄漏?
The entire test code is contained in main.cpp as follows:
#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;
}
I compile with no optimizations -O0
(from an Eclipse Qt project) with:
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
then link as follows:
g++ -Wl,-O0 -o test release/main.o -L/usr/lib -lQtGui -lQtCore -lpthread
I run the executable through valgrind and get the following output:
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)
I believe valgrind should report a memory leak rather than that no leaks are possible because of the heap memory allocated in the calls to new int
EDIT: Changed code above to use std::cout rather than qDebug() with the same result
If I compile and link the same code (from an Eclipse CDT project) with no Qt dependencies, valgrind detects the leak:
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)
I'm using Kubuntu 10.04 32 bit and have tried both Debug and Release builds, what am I doing wrong or why doesn't valgrind report a memory leak when linked against Qt?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我已将 qDebug() 替换为 std::cout:
I have replaced qDebug() with std::cout:
也许您的编译器在优化中消除了该代码,请尝试使用 -O0 进行编译(不带优化选项)。
作为评论,使用 debian lenny 和 -O2,检测到泄漏:
编译器:
Probably your compiler eliminate that code in the optimization, try compiling with -O0 (without optimization options).
As comment, with debian lenny with -O2, the leak is detected:
The compiler:
发生这种情况的原因是调用:
实际上是在 /usr/bin/test 上运行 valgrind,这是一个检查文件类型并比较值的内置程序,我所需要的只是:
The reason this happened is that the call to:
was actually running valgrind on /usr/bin/test which is a built in program that checks file types and compares values, all I needed was: