diff --git a/src/gui/kernel/qopenglcontext.cpp b/src/gui/kernel/qopenglcontext.cpp index dd41318f72a..c25132dc734 100644 --- a/src/gui/kernel/qopenglcontext.cpp +++ b/src/gui/kernel/qopenglcontext.cpp @@ -743,13 +743,13 @@ bool QOpenGLContext::makeCurrent(QSurface *surface) void QOpenGLContext::doneCurrent() { Q_D(QOpenGLContext); - if (!isValid()) - return; - if (QOpenGLContext::currentContext() == this) - d->shareGroup->d_func()->deletePendingResources(this); + if (isValid()) { + if (QOpenGLContext::currentContext() == this) + d->shareGroup->d_func()->deletePendingResources(this); + d->platformGLContext->doneCurrent(); + } - d->platformGLContext->doneCurrent(); QOpenGLContextPrivate::setCurrentContext(nullptr); d->surface = nullptr; diff --git a/tests/auto/gui/qopengl/tst_qopengl.cpp b/tests/auto/gui/qopengl/tst_qopengl.cpp index af59f3e31a3..3f4aa8cd8f9 100644 --- a/tests/auto/gui/qopengl/tst_qopengl.cpp +++ b/tests/auto/gui/qopengl/tst_qopengl.cpp @@ -84,6 +84,9 @@ private slots: void bufferCreate(); void bufferMapRange(); void defaultQGLCurrentBuffer(); +#if QT_CONFIG(egl) + void dontCrashOnInvalidContextThreadTeardown(); +#endif }; struct SharedResourceTracker @@ -1751,6 +1754,31 @@ void tst_QOpenGL::clipRect() //QCOMPARE(fb.pixelColor(clipRect.right(), clipRect.top() + 1), QColor(Qt::red)); } +#if QT_CONFIG(egl) +void tst_QOpenGL::dontCrashOnInvalidContextThreadTeardown() +{ + class Thread : public QThread + { + void run() override + { + auto context = std::make_unique(); + QVERIFY(context->create()); + QScopedPointer surface(createSurface(int(QSurface::Window))); + QVERIFY(context->makeCurrent(surface.data())); + auto eglContext = context->nativeInterface(); + if (!eglContext) { + QSKIP("Need an egl context for this test"); + } + eglContext->invalidateContext(); + context->doneCurrent(); + } + }; + Thread thread; + thread.start(); + thread.wait(); +} +#endif + QTEST_MAIN(tst_QOpenGL) #include "tst_qopengl.moc"