Question

I have a series of QLabel objects on my Qt GUI, and I fill them with HBITMAP objects. These HBITMAP's are buffers in memory, they don't exist on disk.

Now I use the QPixmaps fromWinHBITMAPto create aQPixmapwhich I can then pass to theQLabelssetPixmap` function.

Now, the question is, what happens to the current image in the QLabel, when I overwrite it with another, does it stay in memory? Does it get deleted?

I suspect it doesn't get deleted properly, as my program grows to enormous proportions after running for about an hour. (1.7GB) in memory.

The code which does the conversion is:

//buffer is a map of QLabels which are filled with images.
void LoadPixmapFromBitmap(HBITMAP hBitmap, std::map<int, QLabel*>& buffer, int pixmapindex)
{
    QPixmap pix;
    pix = QPixmap::fromWinHBITMAP(hBitmap);

    QPixmap temp(pix);      
    QSize sz(164, 121);
    QPixmap resized(temp.scaled(sz));

    QMatrix rotation;
    rotation.rotate(90);
    QPixmap rotated = resized.transformed(rotation);

//an attempt to delete the previous image properly and put in a new one.  This doesn't seem to work.
    if (buffer[pixmapindex]->pixmap() != NULL)
    {
        HBITMAP hbtmp = buffer[pixmapindex]->pixmap()->toWinHBITMAP();
        buffer[pixmapindex]->clear();

        HDC dc = GetDC(this->winId());
        //HBITMAP p_old = SelectObject(dc, hbtmp);

        BOOL deleted = DeleteObject(hbtmp);
        if (!deleted)
            PrintMsg("temp not deleted");
    }

//////////////////////////////////end of attempt
    buffer[pixmapindex]->setPixmap(rotated);

    BOOL success = DeleteObject(hBitmap);
    if (!success)
        PrintMsg("hBitmap was not deleted");
}
Was it helpful?

Solution

QPixmap::fromWinHBITMAP make a copy of given bitmap, not alias one.

You should delete original bitmap just after conversion to QPixmap, because call to oWinHBITMAP makes a copy (again) of bitmap, storied in given pixmap, but not gives you handle to original windows bitmap.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top