I'd previously written a basic UI using mostly raw OpenGL which I'm now re-writing using QGraphicsScene for a number of reasons (mouse handling etc).
I've been struggling with a memory leak and think I've tracked it down to Qt, rather than my own code, below is a minimal example which produces the leak on Mac OS 10.6.4 and 10.6.5.
main.cpp:
#include <QtGui/QApplication>
#include <QtGui>
#include <QGraphicsView>
#include <QtOpenGL/QGLWidget>
#include <QtOpenGL/QGLFormat>
#include "mygraphicsscene.h"
int main(int argc, char *argv[])
{
MyGraphicsScene *scene = new MyGraphicsScene;
// NoViewPortUpdate has the same effect
view->setScene(scene);
view->show();
view->resize(1024,768);
return a.exec();
}
#include <QtGui/QApplication>
#include <QtGui>
#include <QGraphicsView>
#include <QtOpenGL/QGLWidget>
#include <QtOpenGL/QGLFormat>
#include "mygraphicsscene.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MyGraphicsScene *scene = new MyGraphicsScene;
QGraphicsView *view = new QGraphicsView;
view->setViewport(new QGLWidget());
view->setViewportUpdateMode(QGraphicsView::FullViewportUpdate);
// NoViewPortUpdate has the same effect
view->setScene(scene);
view->show();
view->resize(1024,768);
return a.exec();
}
To copy to clipboard, switch view to plain text mode
mygraphicsscene.h:
#ifndef MYGRAPHICSSCENE_H
#define MYGRAPHICSSCENE_H
#include <QGraphicsScene>
#include <QTimer>
Q_OBJECT
public:
explicit MyGraphicsScene
(QObject *parent
= 0);
public slots:
void refresh();
protected:
int _frame;
};
#endif // MYGRAPHICSSCENE_H
#ifndef MYGRAPHICSSCENE_H
#define MYGRAPHICSSCENE_H
#include <QGraphicsScene>
#include <QTimer>
class MyGraphicsScene : public QGraphicsScene {
Q_OBJECT
public:
explicit MyGraphicsScene(QObject *parent = 0);
void drawBackground(QPainter *painter, const QRectF &rect);
void drawForeground(QPainter *painter, const QRectF &rect);
public slots:
void refresh();
protected:
QTimer *_refresher;
int _frame;
};
#endif // MYGRAPHICSSCENE_H
To copy to clipboard, switch view to plain text mode
mygraphicsscene.cpp:
#include "MyGraphicsScene.h"
#include <QtCore>
#include <QtGlobal>
#include <QPainter>
#include <QPaintEngine>
#include <QGraphicsItem>
#include <QGraphicsProxyWidget>
#include <QGLWidget>
_frame = 0;
_refresher->setInterval(40); // Update 25FPS
_refresher->setSingleShot(false);
connect(_refresher,SIGNAL(timeout()),this,SLOT(refresh()));
_refresher->start();
}
void MyGraphicsScene
::drawBackground(QPainter *painter,
const QRectF &rect
) {
}
void MyGraphicsScene
::drawForeground(QPainter *painter,
const QRectF &rect
) {
}
void MyGraphicsScene::refresh() {
++_frame;
this->update();
}
#include "MyGraphicsScene.h"
#include <QtCore>
#include <QtGlobal>
#include <QPainter>
#include <QPaintEngine>
#include <QGraphicsItem>
#include <QGraphicsProxyWidget>
#include <QGLWidget>
MyGraphicsScene::MyGraphicsScene(QObject *parent) : QGraphicsScene(parent) {
_frame = 0;
_refresher = new QTimer();
_refresher->setInterval(40); // Update 25FPS
_refresher->setSingleShot(false);
connect(_refresher,SIGNAL(timeout()),this,SLOT(refresh()));
_refresher->start();
}
void MyGraphicsScene::drawBackground(QPainter *painter, const QRectF &rect) {
}
void MyGraphicsScene::drawForeground(QPainter *painter, const QRectF &rect) {
}
void MyGraphicsScene::refresh() {
++_frame;
this->update();
}
To copy to clipboard, switch view to plain text mode
Have I done something wrong or is this a problem with Qt itself?
The first 2 screenshots are from Instruments.app showing the memory leak. It steadily grows by 100KB every 20 seconds or so.
The third attachement shows the vast amount of memory allocated by the code above after just 2MB of leaks (Left to Right: CPU%, Threads, Real Mem). I don't know what's consuming all of that (surely the Qt libraries are less than 20MB?).
Any ideas?
Thanks
Dan
Bookmarks