Here i want to show a really nasty bug in QT’s QPixmap class that might confuse a little bit. I only tested this on Symbian plattform (Nokia N8 on the device itself).

    screenshot = QPixmap::grabWindow(QApplication::desktop()->winId());
    screenshot.scaled(QSize(64,32),Qt::IgnoreAspectRatio,Qt::SmoothTransformation);
    qimg = new QImage(screenshot.toImage());

If i called this 3 lines about 15 times (no matter when) i got an “out of memory” message from QImage class. After a bit of investigation and searching for a solution i’ve found a workaround for this problem.
The error was not created by how i created the QImage but by scaling the QPixmap! The easiest way I found to prevent this memory leak (which isn’t btw because on Symbian i had over 101 mb free ram and over 10 mb free vram) is to convert the QPixmap first to QImage and then scale it to desired size:

    screenshot = QPixmap::grabWindow(QApplication::desktop()->winId());
    qimg = new QImage(screenshot.toImage().scaled(QSize(128,128),Qt::IgnoreAspectRatio,Qt::SmoothTransformation));

Now the memory error never showed up. I hope this helps out a bit because i’ve seen a few similar posts on web.


Leave a Reply

Your email address will not be published. Required fields are marked *