PDA

View Full Version : Report, QTextDocument (memory leak)



layer
23rd March 2012, 02:44
I have some issues with simple reports that my application makes by QTextDocument, html-page (template), QPrinter and QPrintPreviewDialog. I made classes for this stuff.
But recently I was realised that I have memory leak. so I tried to find where is it.
It seems that I found out smth but don't know exactly why this happening..

I threw out spec. classes and wrote just necessary code:



QTextDocument* document;
........
// in widget's constructor
document = new QTextDocument();
........
// in widget'd destructor
delete document;
........

//----------------------------------------------------------------------------------------
void fmPartStatInRange::previewSelectionReport() {
QFile file (QCoreApplication::applicationDirPath () + "/reports/statisticPartsInRange.html");
if (!file.open (QIODevice::ReadOnly)) {
QMessageBox::critical (this, "", tr ("Error opening report file %1").arg (file.fileName ()));
return;
}
QTextStream stream (&file);
stream.setCodec (QTextCodec::codecForName ("UTF-8"));
document->clear();
document->setHtml (stream.readAll ());
file.close ();

//----------------------------------------------------------------------------------------
void fmPartStatInRange::print(QPrinter *printer) {
#ifdef QT_NO_PRINTER
Q_UNUSED(printer);
#else
document->print(printer);
#endif
}

//----------------------------------------------------------------------------------------
void fmPartStatInRange::printReport() {
#ifndef QT_NO_PRINTER
QPrinter printer(QPrinter::HighResolution);
QPrintPreviewDialog preview(&printer, this);
connect(&preview, SIGNAL(paintRequested(QPrinter*)), SLOT(print(QPrinter*)));
preview.exec();
#endif
}

//----------------------------------------------------------------------------------------
void fmPartStatInRange::createReport() {
previewSelectionReport();
if (document)
printReport();
}
//----------------------------------------------------------------------------------------



I execute createReport() many times and see memory leak.
But if I comment this line:

document->setHtml (stream.readAll ());
that's all ok. no memory leak.
of course in this case I don't get any sence of report only white page.

I use 4.7.3 Qt ver.
unfortunately I haven't any opportunities to check out it on new Qt ver.

so what is wrong with this code or maybe it's a bug in Qt lib?

thanks in advance!

ChrisW67
23rd March 2012, 06:34
How do you "see memory leak"?

layer
23rd March 2012, 07:55
in win task manager for example.

here it is 24 Mb in memory before doing smth:
7525

after 20 reports (35 Mb):
7526

and it's keep growing if we do reports...
is it normal or I didn't catch smth?