I'm having a big trouble dealing with a weird behaviour of a derived QGLWidget.

In my QMainWindow I have a QTabs that I'm populating with my QGLWidget subclass.

In my QGLWidget subclass, I'm overpainting the OGL with QPainter.drawImage(...) like in the overpainting example shipped with Qt.

Everything goes well until I change focus from one GLWidget instance to another. When it updates the contents (paintEnvent()), I've noticed that memory consumption of the system, NOT my process is increased (and never released until application exits).

Switching focus from one widget to another an repainting is done very often, and this behaviour is unaceptable for the application (it gets to 4 GB of mem used very quickly -> whole system slowdown->paging->system dies.).

I've been inspecting my process with GDIView, a program that shows the amount of GDI resources process uses in realtime. And I have been able to see how the DC resources increases after every focus/repaint change.

This only occurs when the change of focus (and repaint) is done between QGLWidgets.
Also I've located the code thats messing up all, its this snippet inside the paintEvent() implementation :


// OpenGL rendering code...


glMatrixMode(GL_MODELVIEW);
glPopMatrix();

QPainter painter;

painter.begin(this);
painter.setRenderHint(QPainter::HighQualityAntiali asing);
painter.setPen(QColor(255,0,0,255));
painter.setBrush(QBrush(QColor(0,255,0,255)));

if(pImage->m_InfoOverlay) painter.drawImage(0,0,*(pImage->m_InfoOverlay));
painter.translate(this->width(),this->height());
if(pImage->m_WindowLevelOverlay) painter.drawImage(-pImage->m_WindowLevelOverlay->width(),-pImage->m_WindowLevelOverlay->height(),*(pImage->m_WindowLevelOverlay));

painter.end();



If this code is commented, no new GDI DCs are created (as it should be)

Any idea of what I'm doing wrong?

Thank you in advance.