QImageReader内存泄漏

时间:2015-07-29 09:56:44

标签: qt memory-leaks valgrind qimage

我创建了一个应用程序,其中我使用了大量图像。我使用类QImageReader并通过使用valgrind检查,我在使用QImageReader的函数中有内存泄漏,并且当我多次调用这些函数时,整个泄漏非常重要。

以下是我用valgrind获得的两个输出示例:

第一:

==2510== 4,000,000 bytes in 1 blocks are possibly lost in loss record 16,817 of 16,829
==2510==    at 0xC658: malloc (vg_replace_malloc.c:295)
==2510==    by 0x42A819B: QImageData::create(QSize const&, QImage::Format, int) (in /Users/username/Qt5.3.1/5.3/clang_64/lib/QtGui.framework/Versions/5/QtGui)
==2510==    by 0x42A868E: QImage::QImage(QSize const&, QImage::Format) (in /Users/username/Qt5.3.1/5.3/clang_64/lib/QtGui.framework/Versions/5/QtGui)
==2510==    by 0xF92A572: ???
==2510==    by 0xF92A0AB: ???
==2510==    by 0xF92AF22: ???
==2510==    by 0x42BAD4E: QImageReader::read(QImage*) (in /Users/username/Qt5.3.1/5.3/clang_64/lib/QtGui.framework/Versions/5/QtGui)
==2510==    by 0x42BAB29: QImageReader::read() (in /Users/username/Qt5.3.1/5.3/clang_64/lib/QtGui.framework/Versions/5/QtGui)
==2510==    by 0x10011EA6E: ThumbnailList::addThumbnail(QString) (in ./MyApp.app/Contents/MacOS/MyApp)
==2510==    by 0x10013744D: Class1::init() (in ./MyApp.app/Contents/MacOS/MyApp)
==2510==    by 0x1001367B1: Class1::Class1(MainWindow*, Controller*) (in ./MyApp.app/Contents/MacOS/MyApp)
==2510==    by 0x100009E19: MainWindow::MainWindow(QWidget*) (in ./MyApp.app/Contents/MacOS/MyApp)

以及相应的功能:

void ThumbnailList::addThumbnail(QString _filePath)
{
    QImageReader reader(_filePath);
    int faceWidth=reader.size().width();
    int faceHeight=reader.size().height();

    QImage globalImg;
    globalImg=QImage(faceWidth/2,faceHeight/2,reader.imageFormat());
    globalImg=reader.read();

    QPixmap thumb=QPixmap(QPixmap::fromImage(globalImg));
    MyCustomItem *pieceItem = new MyCustomItem(this);
    pieceItem->setIcon(QIcon(thumb));

    pieceItem->setText("thumb");
}

第二:

==2510== 16,777,216 bytes in 1 blocks are possibly lost in loss record 16,823 of 16,829
==2510==    at 0xC658: malloc (vg_replace_malloc.c:295)
==2510==    by 0x42A819B: QImageData::create(QSize const&, QImage::Format, int) (in /Users/username/Qt5.3.1/5.3/clang_64/lib/QtGui.framework/Versions/5/QtGui)
==2510==    by 0x42A868E: QImage::QImage(QSize const&, QImage::Format) (in /Users/username/Qt5.3.1/5.3/clang_64/lib/QtGui.framework/Versions/5/QtGui)
==2510==    by 0xF92A572: ???
==2510==    by 0xF92A0AB: ???
==2510==    by 0xF92AF22: ???
==2510==    by 0x42BAD4E: QImageReader::read(QImage*) (in /Users/username/Qt5.3.1/5.3/clang_64/lib/QtGui.framework/Versions/5/QtGui)
==2510==    by 0x42BAB29: QImageReader::read() (in /Users/username/Qt5.3.1/5.3/clang_64/lib/QtGui.framework/Versions/5/QtGui)
==2510==    by 0x1000F3D4B: Controller::computeOneImage(QString, int, QImage&) (in ./MyApp.app/Contents/MacOS/MyApp)
==2510==    by 0x1000F2813: Controller::computeImages(QString, QString, QImage&, QImage&, QImage&, QImage&, QImage&, QImage&, QImage&, QImage&, QImage&) (in ./MyApp.app/Contents/MacOS/MyApp)
==2510==    by 0x100165CF4: Class2::computeImages() (in ./MyApp.app/Contents/MacOS/MyApp)
==2510==    by 0x100076E00: Controller::openProject(QString, bool) (in ./MyApp.app/Contents/MacOS/MyApp)

相应的功能:

void Controller::computeOneImage(QString inFileImage, int inFaceDim, QImage &outImage)
{
    QImageReader reader(inFileImage);
    if(reader.size().width()>2048)
        reader.setScaledSize(QSize(2048,2048));

    outImage=reader.read();
}

我认为没有一种特殊方法来销毁QImageReader实例,但我可能错了。那么为什么这些函数会出现内存泄漏?

0 个答案:

没有答案