StringTable: Make it QObject-less

Replace a QTimer field with a call to single shot timer,
guarded by qApp.

Make StringTablePrivate QObject-less.

This fixes a remaining warning about destroying QTimer
object after the qApp is gone.

Change-Id: Id71bc17fc72894ad30d277c03e3f09232050d6bb
Reviewed-by: Eike Ziller <eike.ziller@qt.io>
This commit is contained in:
Jarek Kobus
2025-02-04 15:32:31 +01:00
parent d301804992
commit 51bf2d2c68

View File

@@ -5,6 +5,7 @@
#include "async.h" #include "async.h"
#include <QCoreApplication>
#include <QDebug> #include <QDebug>
#include <QElapsedTimer> #include <QElapsedTimer>
#include <QMutex> #include <QMutex>
@@ -15,21 +16,21 @@
// thread and execute destructor of StringTable in main thread. In this case the test should // thread and execute destructor of StringTable in main thread. In this case the test should
// ensure that destructor of StringTable waits for its internal thread to finish. // ensure that destructor of StringTable waits for its internal thread to finish.
namespace Utils::StringTable { using namespace std::chrono;
enum { namespace Utils::StringTable {
GCTimeOut = 10 * 1000 // 10 seconds
};
enum { enum {
DebugStringTable = 0 DebugStringTable = 0
}; };
class StringTablePrivate : public QObject static std::atomic_bool s_isScheduled = false;
class StringTablePrivate
{ {
public: public:
StringTablePrivate(); StringTablePrivate() { m_strings.reserve(1000); }
~StringTablePrivate() override { cancelAndWait(); } ~StringTablePrivate() { cancelAndWait(); }
void cancelAndWait(); void cancelAndWait();
QString insert(const QString &string); QString insert(const QString &string);
@@ -39,7 +40,6 @@ public:
QFuture<void> m_future; QFuture<void> m_future;
QMutex m_lock; QMutex m_lock;
QSet<QString> m_strings; QSet<QString> m_strings;
QTimer m_gcCountDown;
}; };
static StringTablePrivate &stringTable() static StringTablePrivate &stringTable()
@@ -48,16 +48,6 @@ static StringTablePrivate &stringTable()
return theStringTable; return theStringTable;
} }
StringTablePrivate::StringTablePrivate()
{
m_strings.reserve(1000);
m_gcCountDown.setObjectName(QLatin1String("StringTable::m_gcCountDown"));
m_gcCountDown.setSingleShot(true);
m_gcCountDown.setInterval(GCTimeOut);
connect(&m_gcCountDown, &QTimer::timeout, this, &StringTablePrivate::startGC);
}
QTCREATOR_UTILS_EXPORT QString insert(const QString &string) QTCREATOR_UTILS_EXPORT QString insert(const QString &string)
{ {
return stringTable().insert(string); return stringTable().insert(string);
@@ -88,6 +78,7 @@ QString StringTablePrivate::insert(const QString &string)
void StringTablePrivate::startGC() void StringTablePrivate::startGC()
{ {
s_isScheduled.exchange(false);
QMutexLocker locker(&m_lock); QMutexLocker locker(&m_lock);
cancelAndWait(); cancelAndWait();
m_future = Utils::asyncRun(&StringTablePrivate::GC, this); m_future = Utils::asyncRun(&StringTablePrivate::GC, this);
@@ -95,8 +86,8 @@ void StringTablePrivate::startGC()
QTCREATOR_UTILS_EXPORT void scheduleGC() QTCREATOR_UTILS_EXPORT void scheduleGC()
{ {
QMetaObject::invokeMethod(&stringTable().m_gcCountDown, QOverload<>::of(&QTimer::start), if (!s_isScheduled.exchange(true))
Qt::QueuedConnection); QTimer::singleShot(10s, qApp, [] { stringTable().startGC(); });
} }
static int bytesSaved = 0; static int bytesSaved = 0;