QImage 和 valgrind ;内存泄漏 [英] QImage and valgrind ; memory leak

查看:141
本文介绍了QImage 和 valgrind ;内存泄漏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对以下代码有疑问:

#include int main(int argc, char* argv[]) {QImage qimage(100, 100, QImage::Format_ARGB32);qimage.fill(Qt::white);qimage.save("test.png", "PNG", 70);返回0;}

编译如下:

gcc -I/usr/include/qt4 test.cpp -lQtGui

该代码生成了正确的图像.但是,当我 valgrind 时:

valgrind --leak-check=full ./a.out

它会产生一系列丢失的块,如下所示:

==5974== 158(56 个直接,102 个间接)字节在 1 个块中的丢失记录 54 of 79 中肯定丢失==5974== 在 0x402B9B4:operator new(unsigned int)(在/usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)==5974== 由 0x4E4E427: QLibraryPrivate::findOrCreate(QString const&, QString const&) (qlibrary.cpp:437)==5974== by 0x4E4E721:QLibrary::setFileNameAndVersion(QString const&, QString const&) (qlibrary.cpp:1110)==5974== 由 0x56290DF: ???

或者这个:

==5974== 1 个块中的 396(56 个直接,340 个间接)字节在丢失记录 61 of 79 中肯定丢失==5974== 在 0x402B9B4:operator new(unsigned int)(在/usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)==5974== 由 0x4E4E427: QLibraryPrivate::findOrCreate(QString const&, QString const&) (qlibrary.cpp:437)==5974== 由 0x4E44FB6: QFactoryLoader::updateDir(QString const&, QSettings&) (qfactoryloader.cpp:145)==5974== 由 0x56F9E67: ???

我给你完整的日志,你可以在家里执行相同的:)

我试图理解,它们似乎在于我在 QImageWriter 的源代码中找到的以下几行:

QFactoryLoader *l = loader();QStringList keys = l->keys();

在我看来,当您第一次尝试生成 PNG 图像时,它构建的内容将在您每次构建 PNG 图像时重复使用,并且永远不会释放内存.QFactoryLoader 的析构函数似乎知道如何清理东西,我很想亲自做一个 delete l; 但没有办法调用它,因为 QFactoryLoader 是 Qt 的私有实现.>

有人可能会争辩说这不是真正的内存泄漏,因为每种图像格式只有一个键,但恕我直言,做事的干净方法是能够在退出之前清除所有内容.

所以我的问题是:有没有办法做到这一点?

解决方案

Qt 的图像 I/O 功能基于插件.当你请求一个 QImage 来加载一个 png 文件时,PNG 库被加载为一个 QObject once,直到程序退出它才会被卸载- 这就是 Valgrind 认为的内存泄漏.

I have an issue with the following code:

#include <QtGui/QImage>

int main(int argc, char* argv[]) {
  QImage qimage(100, 100, QImage::Format_ARGB32);
  qimage.fill(Qt::white);
  qimage.save("test.png", "PNG", 70);

  return 0;
}

to be compiled as followed:

gcc -I/usr/include/qt4 test.cpp -lQtGui

The code generates a proper image. However, when I valgrind it:

valgrind --leak-check=full ./a.out

it yields a series of lost blocks, like the following one:

==5974== 158 (56 direct, 102 indirect) bytes in 1 blocks are definitely lost in loss record 54 of 79
==5974==    at 0x402B9B4: operator new(unsigned int) (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
==5974==    by 0x4E4E427: QLibraryPrivate::findOrCreate(QString const&, QString const&) (qlibrary.cpp:437)
==5974==    by 0x4E4E721: QLibrary::setFileNameAndVersion(QString const&, QString const&) (qlibrary.cpp:1110)
==5974==    by 0x56290DF: ???

or this one :

==5974== 396 (56 direct, 340 indirect) bytes in 1 blocks are definitely lost in loss record 61 of 79
==5974==    at 0x402B9B4: operator new(unsigned int) (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
==5974==    by 0x4E4E427: QLibraryPrivate::findOrCreate(QString const&, QString const&) (qlibrary.cpp:437)
==5974==    by 0x4E44FB6: QFactoryLoader::updateDir(QString const&, QSettings&) (qfactoryloader.cpp:145)
==5974==    by 0x56F9E67: ???

I spare you the full log, you can just execute the same at home :)

I tried to understand and they seems to lie in the following lines I found in the source code for QImageWriter:

QFactoryLoader *l = loader();
QStringList keys = l->keys();

It seems to me that when you first try to generate a PNG image, it builds something that will be reused each time you will build a PNG image and that memory is never freed. The destructor for QFactoryLoader seems to know how to clean things, and I would love to personally do a delete l; but there is no way to call it since QFactoryLoader is private to the implementation of Qt.

One could argue that this is not a real memory leak since there would be only one key per image format, but imho, the clean way to do things would be to be able to clear everything before quitting.

So my question: is there any way to do that?

解决方案

Qt's image I/O functionality is based around plugins. When you request a QImage to a load a png file the PNG library is loaded as a QObject once, it is not unloaded until the program exits - this is what Valgrind sees as a memory leak.

这篇关于QImage 和 valgrind ;内存泄漏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆