tags:

views:

82

answers:

1

Hello everyone!

I have a question about memory leaks in Qt.

I have a QMainWindow with 2 QPushButtons.

First button click signal:

m_label = new QLabel(this);

QPixmap pix(this->size());
QPainter painter;
painter.begin(&pix);

QImage img("1.png");
painter.drawPixmap(this->rect(), QPixmap::fromImage(img));
m_label->setPixmap(pix);

painter.end();

Secont button click signal:

delete m_label;

When I start my test application the allocated memory is about 11900 Kb When I click on first button then allocated memory for app is about 12450 Kb When I click on the second button I got allocated memory about 12250 Kb

Why I didn't get the same 11900 Kb? Is this a leak?

So if to write the following code:

QImage img("1.png");
QImage img1("1.png");
QImage img2("1.png");
QImage img3("1.png");
QImage img4("1.png");
QImage img5("1.png");
QImage img6("1.png");
QImage img7("1.png");
QImage img8("1.png");
QImage img9("1.png");

Then allocated memory grows but doesn't decrease. Why? How to clean this memory leak?

+3  A: 
  1. After first call of QImage img("1.png") Qt loads image formats plugins, and leaves them loaded after use (for performance issues). So part of memory that's left is from loaded plugins, and it's not a leak.
  2. Where do you allocate those QImages (img...img9 case)? Are you sure that they go out of scope?
  3. Remember that OS doesn't always retain memory immediately after freeing it by your software.
Kamil Klimek
Also, I think Qt caches some data, along with the fact that many memory structures that would get triggered by creation of an object will usually only grow, and not shrink very often (like the list of child objects, for example).
Caleb Huitt - cjhuitt