2

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?

2
  • 2
    How are you measuring memory comsumption?
    – Dr. Snoopy
    Oct 5, 2010 at 17:59
  • ...and when would you expect it to decrease in the img...img9 case? (But the general measuring question is more relevant) Oct 5, 2010 at 20:48

1 Answer 1

3
  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.
1
  • 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). Oct 6, 2010 at 14:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.