From 5dc4004afc05f2ccfda3421a61b9f91c5cbcc640 Mon Sep 17 00:00:00 2001 From: Mike Achtelik Date: Tue, 9 Jun 2020 13:55:15 +0200 Subject: Fix living QLibrary member after shutdown of QCoreApplication LibResolv uses a QLibrary which is a QObject that must be deleted if the QCoreApplication is being destroyed to release the underlying library. A Q_GLOBAL_STATIC won't release any memory and is not able to manually release it. Pick-to: 5.15 Task-number: QTBUG-84234 Change-Id: I97fe5faca309e9c1e85435f602ad7f8c3f633b48 Reviewed-by: Thiago Macieira --- src/network/kernel/qhostinfo_unix.cpp | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/src/network/kernel/qhostinfo_unix.cpp b/src/network/kernel/qhostinfo_unix.cpp index 625fbabf31..9b0a2ee669 100644 --- a/src/network/kernel/qhostinfo_unix.cpp +++ b/src/network/kernel/qhostinfo_unix.cpp @@ -156,7 +156,25 @@ LibResolv::LibResolv() } } } -Q_GLOBAL_STATIC(LibResolv, libResolv) + +LibResolv* libResolv() +{ + static LibResolv* theLibResolv = nullptr; + static QBasicMutex theMutex; + + const QMutexLocker locker(&theMutex); + if (theLibResolv == nullptr) { + theLibResolv = new LibResolv(); + Q_ASSERT(QCoreApplication::instance()); + QObject::connect(QCoreApplication::instance(), &QCoreApplication::destroyed, [] { + const QMutexLocker locker(&theMutex); + delete theLibResolv; + theLibResolv = nullptr; + }); + } + + return theLibResolv; +} static void resolveLibrary(LibResolvFeature f) { -- cgit v1.2.3