From 57f243594e83708f2daa8147901cbea965e5c295 Mon Sep 17 00:00:00 2001 From: Friedemann Kleint Date: Fri, 25 May 2012 12:50:21 +0200 Subject: [PATCH] OpenGL/HelloWindow example: Fix resource leaks. Change-Id: Icf776b5581b3a632e8cef19af1059503146c158c Reviewed-by: Girish Ramakrishnan --- examples/opengl/hellowindow/hellowindow.cpp | 6 +++--- examples/opengl/hellowindow/hellowindow.h | 5 +++-- examples/opengl/hellowindow/main.cpp | 20 +++++++++++++++----- 3 files changed, 21 insertions(+), 10 deletions(-) diff --git a/examples/opengl/hellowindow/hellowindow.cpp b/examples/opengl/hellowindow/hellowindow.cpp index b7b3d2b10e..29e128e248 100644 --- a/examples/opengl/hellowindow/hellowindow.cpp +++ b/examples/opengl/hellowindow/hellowindow.cpp @@ -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) : 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(); diff --git a/examples/opengl/hellowindow/hellowindow.h b/examples/opengl/hellowindow/hellowindow.h index 8f294b5ae3..0a88a0d75e 100644 --- a/examples/opengl/hellowindow/hellowindow.h +++ b/examples/opengl/hellowindow/hellowindow.h @@ -45,6 +45,7 @@ #include #include +#include QT_BEGIN_NAMESPACE class QOpenGLContext; @@ -88,7 +89,7 @@ class HelloWindow : public QWindow { Q_OBJECT public: - HelloWindow(Renderer *renderer); + explicit HelloWindow(const QSharedPointer &renderer); void updateColor(); @@ -105,5 +106,5 @@ private: int m_colorIndex; QColor m_color; - Renderer *m_renderer; + const QSharedPointer m_renderer; }; diff --git a/examples/opengl/hellowindow/main.cpp b/examples/opengl/hellowindow/main.cpp index e1fde9b5b8..1c11639ebc 100644 --- a/examples/opengl/hellowindow/main.cpp +++ b/examples/opengl/hellowindow/main.cpp @@ -66,15 +66,18 @@ int main(int argc, char **argv) QSize windowSize(400, 320); int delta = 40; - Renderer *rendererA = new Renderer(format); + QList windows; + QSharedPointer 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 renderThreads; if (multipleWindows) { - Renderer *rendererB = new Renderer(format, rendererA); + QSharedPointer 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(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; }