OpenGL/HelloWindow example: Fix resource leaks.

Change-Id: Icf776b5581b3a632e8cef19af1059503146c158c
Reviewed-by: Girish Ramakrishnan <girish.1.ramakrishnan@nokia.com>
This commit is contained in:
Friedemann Kleint 2012-05-25 12:50:21 +02:00 committed by Qt by Nokia
parent 7381328bcc
commit 57f243594e
3 changed files with 21 additions and 10 deletions

View File

@ -59,7 +59,7 @@ Renderer::Renderer(const QSurfaceFormat &format, Renderer *share, QScreen *scree
m_context->create();
}
HelloWindow::HelloWindow(Renderer *renderer)
HelloWindow::HelloWindow(const QSharedPointer<Renderer> &renderer)
: m_colorIndex(0)
, m_renderer(renderer)
{
@ -73,7 +73,7 @@ HelloWindow::HelloWindow(Renderer *renderer)
create();
connect(this, SIGNAL(needRender(QSurface *, const QColor &, const QSize &)),
renderer, SLOT(render(QSurface *, const QColor &, const QSize &)));
renderer.data(), SLOT(render(QSurface *, const QColor &, const QSize &)));
updateColor();
}
@ -196,7 +196,7 @@ void Renderer::initialize()
"}\n";
fshader->compileSourceCode(fsrc);
m_program = new QOpenGLShaderProgram;
m_program = new QOpenGLShaderProgram(this);
m_program->addShader(vshader);
m_program->addShader(fshader);
m_program->link();

View File

@ -45,6 +45,7 @@
#include <QColor>
#include <QTime>
#include <QSharedPointer>
QT_BEGIN_NAMESPACE
class QOpenGLContext;
@ -88,7 +89,7 @@ class HelloWindow : public QWindow
{
Q_OBJECT
public:
HelloWindow(Renderer *renderer);
explicit HelloWindow(const QSharedPointer<Renderer> &renderer);
void updateColor();
@ -105,5 +106,5 @@ private:
int m_colorIndex;
QColor m_color;
Renderer *m_renderer;
const QSharedPointer<Renderer> m_renderer;
};

View File

@ -66,15 +66,18 @@ int main(int argc, char **argv)
QSize windowSize(400, 320);
int delta = 40;
Renderer *rendererA = new Renderer(format);
QList<QWindow *> windows;
QSharedPointer<Renderer> rendererA(new Renderer(format));
HelloWindow *windowA = new HelloWindow(rendererA);
windowA->setGeometry(QRect(center, windowSize).translated(-windowSize.width() - delta / 2, 0));
windowA->setWindowTitle(QLatin1String("Thread A - Context A"));
windowA->setVisible(true);
windows.prepend(windowA);
QList<QThread *> renderThreads;
if (multipleWindows) {
Renderer *rendererB = new Renderer(format, rendererA);
QSharedPointer<Renderer> rendererB(new Renderer(format, rendererA.data()));
QThread *renderThread = new QThread;
rendererB->moveToThread(renderThread);
@ -84,19 +87,21 @@ int main(int argc, char **argv)
windowB->setGeometry(QRect(center, windowSize).translated(delta / 2, 0));
windowB->setWindowTitle(QLatin1String("Thread A - Context A"));
windowB->setVisible(true);
windows.prepend(windowB);
HelloWindow *windowC = new HelloWindow(rendererB);
windowC->setGeometry(QRect(center, windowSize).translated(-windowSize.width() / 2, windowSize.height() + delta));
windowC->setWindowTitle(QLatin1String("Thread B - Context B"));
windowC->setVisible(true);
windows.prepend(windowC);
for (int i = 1; i < QGuiApplication::screens().size(); ++i) {
QScreen *screen = QGuiApplication::screens().at(i);
Renderer *renderer = new Renderer(format, rendererA, screen);
QSharedPointer<Renderer> renderer(new Renderer(format, rendererA.data(), screen));
renderThread = new QThread;
renderer->moveToThread(renderThread);
renderThreads << renderThread;
renderThreads.prepend(renderThread);
QRect screenGeometry = screen->availableGeometry();
QPoint center = screenGeometry.center();
@ -110,6 +115,7 @@ int main(int argc, char **argv)
QChar id = QChar('B' + i);
window->setWindowTitle(QLatin1String("Thread ") + id + QLatin1String(" - Context ") + id);
window->setVisible(true);
windows.prepend(window);
}
}
@ -118,8 +124,12 @@ int main(int argc, char **argv)
renderThreads.at(i)->start();
}
app.exec();
const int exitValue = app.exec();
for (int i = 0; i < renderThreads.size(); ++i)
renderThreads.at(i)->wait();
qDeleteAll(windows);
qDeleteAll(renderThreads);
return exitValue;
}