From 03ab94b0e7a549bcf839639438f782a47522ffa7 Mon Sep 17 00:00:00 2001 From: Igor Mironchik Date: Thu, 24 May 2018 15:59:23 +0300 Subject: [PATCH] Deregister QTextCodec on destruction QTextCodec automatically deregisters on destruction now. [ChangeLog][QtCore][QTextCodec] QTextCodec automatically deregisters on destruction now. Task-number: QTBUG-56203 Change-Id: Ic9a66c512642c9913aa27ea5167b9f7341e7f0fe Reviewed-by: Thiago Macieira --- src/corelib/codecs/qtextcodec.cpp | 18 ++++++++++++++++++ src/corelib/kernel/qcoreglobaldata.cpp | 5 ++++- .../codecs/qtextcodec/tst_qtextcodec.cpp | 7 ++++++- 3 files changed, 28 insertions(+), 2 deletions(-) diff --git a/src/corelib/codecs/qtextcodec.cpp b/src/corelib/codecs/qtextcodec.cpp index 1541c498e6..7951e95db5 100644 --- a/src/corelib/codecs/qtextcodec.cpp +++ b/src/corelib/codecs/qtextcodec.cpp @@ -492,6 +492,24 @@ QTextCodec::QTextCodec() */ QTextCodec::~QTextCodec() { + QCoreGlobalData *globalData = QCoreGlobalData::instance(); + if (!globalData) + return; + + globalData->codecForLocale.testAndSetRelaxed(this, nullptr); + + QMutexLocker locker(textCodecsMutex()); + + globalData->allCodecs.removeOne(this); + + auto it = globalData->codecCache.cbegin(); + + while (it != globalData->codecCache.cend()) { + if (it.value() == this) + it = globalData->codecCache.erase(it); + else + ++it; + } } /*! diff --git a/src/corelib/kernel/qcoreglobaldata.cpp b/src/corelib/kernel/qcoreglobaldata.cpp index e2087b9e64..88a45ef4ee 100644 --- a/src/corelib/kernel/qcoreglobaldata.cpp +++ b/src/corelib/kernel/qcoreglobaldata.cpp @@ -55,7 +55,10 @@ QCoreGlobalData::~QCoreGlobalData() { #if QT_CONFIG(textcodec) codecForLocale = 0; - for (QList::const_iterator it = allCodecs.constBegin(); it != allCodecs.constEnd(); ++it) + QList tmp = allCodecs; + allCodecs.clear(); + codecCache.clear(); + for (QList::const_iterator it = tmp.constBegin(); it != tmp.constEnd(); ++it) delete *it; #endif } diff --git a/tests/auto/corelib/codecs/qtextcodec/tst_qtextcodec.cpp b/tests/auto/corelib/codecs/qtextcodec/tst_qtextcodec.cpp index f8f9387abb..b3b7c082cc 100644 --- a/tests/auto/corelib/codecs/qtextcodec/tst_qtextcodec.cpp +++ b/tests/auto/corelib/codecs/qtextcodec/tst_qtextcodec.cpp @@ -2429,7 +2429,7 @@ void tst_QTextCodec::userCodec() QVERIFY(!QTextCodec::availableCodecs().contains("UserCodec")); QVERIFY(!QTextCodec::codecForName("UserCodec")); - QTextCodec *codec = new UserCodec; + UserCodec *codec = new UserCodec; executedOnce = true; QList availableCodecs = QTextCodec::availableCodecs(); @@ -2448,6 +2448,11 @@ void tst_QTextCodec::userCodec() pcodec = QTextCodec::codecForMib(5000); QCOMPARE(pcodec, codec); + + delete codec; + + pcodec = QTextCodec::codecForName("UserCodec"); + QCOMPARE(pcodec, nullptr); } struct DontCrashAtExit {