From 19ccbdabfe889fccd67ab162bbb144846ab098fb Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Fri, 24 May 2019 22:06:09 +0200 Subject: QWidget: replace manual memory management with unique_ptr [3/N]: topextra->shareContext MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Despite the name, it's fully owned by an individual QWidget object. Also make the member mutable, so we can remove the const_cast hack in QWidgetPrivate::shareContext(), and protect QT_NO_OPENGL builds, since the naked pointer compiled by chance due to some unguarded forward declarations while a unique_ptr will somewhere want to call the dtor, which doesn't compile on an object of merely forward-declared type. Change-Id: If8027b55d303822236fcdc1a79e4f3010967b4d2 Reviewed-by: MÃ¥rten Nordheim --- src/widgets/kernel/qwidget.cpp | 11 ++++------- src/widgets/kernel/qwidget_p.h | 4 +++- 2 files changed, 7 insertions(+), 8 deletions(-) (limited to 'src') diff --git a/src/widgets/kernel/qwidget.cpp b/src/widgets/kernel/qwidget.cpp index 68ce8d5d15..78a8af62df 100644 --- a/src/widgets/kernel/qwidget.cpp +++ b/src/widgets/kernel/qwidget.cpp @@ -1799,7 +1799,6 @@ void QWidgetPrivate::createTLExtra() x->inTopLevelResize = false; x->embedded = 0; x->window = 0; - x->shareContext = 0; x->initialScreenIndex = -1; #if 0 // Used to be included in Qt4 for Q_WS_MAC x->wasMaximized = false; @@ -1903,8 +1902,7 @@ void QWidgetPrivate::deleteTLSysExtra() deleteBackingStore(this); #ifndef QT_NO_OPENGL extra->topextra->widgetTextures.clear(); - delete extra->topextra->shareContext; - extra->topextra->shareContext = 0; + extra->topextra->shareContext.reset(); #endif //the toplevel might have a context with a "qglcontext associated with it. We need to @@ -12413,16 +12411,15 @@ QOpenGLContext *QWidgetPrivate::shareContext() const if (Q_UNLIKELY(!extra || !extra->topextra || !extra->topextra->window)) return 0; - QWidgetPrivate *that = const_cast(this); if (!extra->topextra->shareContext) { - QOpenGLContext *ctx = new QOpenGLContext; + auto ctx = qt_make_unique(); ctx->setShareContext(qt_gl_global_share_context()); ctx->setFormat(extra->topextra->window->format()); ctx->setScreen(extra->topextra->window->screen()); ctx->create(); - that->extra->topextra->shareContext = ctx; + extra->topextra->shareContext = std::move(ctx); } - return that->extra->topextra->shareContext; + return extra->topextra->shareContext.get(); #endif // QT_NO_OPENGL } diff --git a/src/widgets/kernel/qwidget_p.h b/src/widgets/kernel/qwidget_p.h index 435aae2950..b43489a98f 100644 --- a/src/widgets/kernel/qwidget_p.h +++ b/src/widgets/kernel/qwidget_p.h @@ -169,7 +169,9 @@ struct QTLWExtra { QBackingStore *backingStore; QPainter *sharedPainter; QWidgetWindow *window; - QOpenGLContext *shareContext; +#ifndef QT_NO_OPENGL + mutable std::unique_ptr shareContext; +#endif // Implicit pointers (shared_null). QString caption; // widget caption -- cgit v1.2.3