QImage构造函数内存泄漏(valgrind)

时间:2013-01-28 21:22:08

标签: image qt valgrind memory-leaks

我有一个使用某些Qt类的控制台程序。以下代码是其片段

std::cout << "before the problem (PVM constructor)" << std::endl;
image = new QImage(filename);
std::cout << "after the problem (PVM constructor)" << std::endl;

对valgrind运行它会引发一些错误:

before the problem (PVM constructor)
==8344== Conditional jump or move depends on uninitialised value(s)
==8344==    at 0x51D6D19: inflateReset2 (in /lib/i386-linux-gnu/libz.so.1.2.3.4)
==8344==    by 0x51D6E0C: inflateInit2_ (in /lib/i386-linux-gnu/libz.so.1.2.3.4)
==8344==    by 0x51D6E92: inflateInit_ (in /lib/i386-linux-gnu/libz.so.1.2.3.4)
==8344==    by 0x51B3F60: png_create_read_struct_2 (in /lib/i386-linux-gnu/libpng12.so.0.46.0)
==8344==    by 0x42807AC: QImageReader::read(QImage*) (in /usr/lib/i386-linux-gnu/libQtGui.so.4.7.4)
==8344==    by 0x4021BAF: ???
==8344==  Uninitialised value was created by a heap allocation
==8344==    at 0x4028876: malloc (vg_replace_malloc.c:236)
==8344==    by 0x51C0796: png_malloc_default (in /lib/i386-linux-gnu/libpng12.so.0.46.0)
==8344==    by 0x51C082C: png_malloc (in /lib/i386-linux-gnu/libpng12.so.0.46.0)
==8344==    by 0x51A6A48: ??? (in /lib/i386-linux-gnu/libpng12.so.0.46.0)
==8344==    by 0x51D6DE8: inflateInit2_ (in /lib/i386-linux-gnu/libz.so.1.2.3.4)
==8344==    by 0x51D6E92: inflateInit_ (in /lib/i386-linux-gnu/libz.so.1.2.3.4)
==8344==    by 0x51B3F60: png_create_read_struct_2 (in /lib/i386-linux-gnu/libpng12.so.0.46.0)
==8344==    by 0x42807AC: QImageReader::read(QImage*) (in /usr/lib/i386-linux-gnu/libQtGui.so.4.7.4)
==8344==    by 0x4021BAF: ???
==8344== 
after the problem (PVM constructor)

你能帮我找一下问题的原因吗?可能是内部Qt库有一些危险的结构 - 或者是在控制台程序中使用Qt GUI lib的问题(不是这么认为,但是谁知道)?

1 个答案:

答案 0 :(得分:3)

Valgrind会告诉你它是否发现了内存泄漏,就像你在堆上分配内存但你永远不会释放它一样。我猜你的程序完成之后你就不会打电话给delete image。这就是valgrind会把它当作内存泄漏的东西。它与Qt无关。 C ++中的规则是:对于每个新的,都有1个删除。