[Qt-interest] memory leak in Qt plugin system?

liang jian jianliang79 at gmail.com
Thu Oct 1 08:13:13 CEST 2009


    I think there is a memory leak in Qt plugin system. This leak
can't be easily produced by the following code:

#include <QtGui/QApplication>
#include <QtGui/QWidget>
#include <QtGui/QImageReader>

int main(int argc, char *argv[])
{
	QApplication a(argc, argv);

	QWidget w;
	w.show();

	QList<QByteArray> fmt = QImageReader::supportedImageFormats();

	int ret = a.exec();

	// Get the current bits
	int tmp = _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG);
	tmp |= _CRTDBG_LEAK_CHECK_DF;
	// Set the new bits
	_CrtSetDbgFlag(tmp);

	return ret;
}

    if you have a windows system and build a debug version of the
above program with visual studio, the memory leak will be reported in
visual studio's output window after you exit the program (you must
press F5 to start the program).
    The memory leak is caused by the line:
    QList<QByteArray> fmt = QImageReader::supportedImageFormats();
    this line will search all the image plugins and load them to
collect supported image formats. For each plugin it will create a root
component of the plugin and after collected information from this root
component it will leave it undeleted. This behaviour is not the reason
of the memory leak because according to Qt's manual "When the library
is finally unloaded, the root component will automatically be
deleted". But the fact is that even after the plugin dll is unloaded
from the current process the root component is not deleted at all. And
this leak will be detected by debug version of C runtime library as
the above program showed to us.



More information about the Qt-interest-old mailing list