From 79832f2156745ee2c6608ce7425fb2350b56f18d Mon Sep 17 00:00:00 2001 From: Bea Lam Date: Fri, 9 Apr 2010 16:01:26 +1000 Subject: Make sure WorkerScript thread is stopped on deletion. This also fixes the leaking of the worker agent in ListModel. --- src/declarative/qml/qdeclarativeworkerscript.cpp | 13 +++++++++++++ .../qdeclarativelistmodel/tst_qdeclarativelistmodel.cpp | 3 --- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/src/declarative/qml/qdeclarativeworkerscript.cpp b/src/declarative/qml/qdeclarativeworkerscript.cpp index caf680e..9e137b5 100644 --- a/src/declarative/qml/qdeclarativeworkerscript.cpp +++ b/src/declarative/qml/qdeclarativeworkerscript.cpp @@ -107,6 +107,10 @@ class QDeclarativeWorkerScriptEnginePrivate : public QObject { Q_OBJECT public: + enum WorkerEventTypes { + WorkerDestroyEvent = QEvent::User + 100 + }; + QDeclarativeWorkerScriptEnginePrivate(QDeclarativeEngine *eng); struct ScriptEngine : public QDeclarativeScriptEngine @@ -159,6 +163,9 @@ public: static QScriptValue onMessage(QScriptContext *ctxt, QScriptEngine *engine); static QScriptValue sendMessage(QScriptContext *ctxt, QScriptEngine *engine); +signals: + void stopThread(); + protected: virtual bool event(QEvent *); @@ -246,6 +253,9 @@ bool QDeclarativeWorkerScriptEnginePrivate::event(QEvent *event) WorkerLoadEvent *workerEvent = static_cast(event); processLoad(workerEvent->workerId(), workerEvent->url()); return true; + } else if (event->type() == (QEvent::Type)WorkerDestroyEvent) { + emit stopThread(); + return true; } else { return QObject::event(event); } @@ -429,6 +439,7 @@ QDeclarativeWorkerScriptEngine::QDeclarativeWorkerScriptEngine(QDeclarativeEngin : QThread(parent), d(new QDeclarativeWorkerScriptEnginePrivate(parent)) { d->m_lock.lock(); + connect(d, SIGNAL(stopThread()), this, SLOT(quit()), Qt::DirectConnection); start(QThread::LowPriority); d->m_wait.wait(&d->m_lock); d->moveToThread(this); @@ -440,8 +451,10 @@ QDeclarativeWorkerScriptEngine::~QDeclarativeWorkerScriptEngine() d->m_lock.lock(); qDeleteAll(d->workers); d->workers.clear(); + QCoreApplication::postEvent(d, new QEvent((QEvent::Type)QDeclarativeWorkerScriptEnginePrivate::WorkerDestroyEvent)); d->m_lock.unlock(); + wait(); d->deleteLater(); } diff --git a/tests/auto/declarative/qdeclarativelistmodel/tst_qdeclarativelistmodel.cpp b/tests/auto/declarative/qdeclarativelistmodel/tst_qdeclarativelistmodel.cpp index 8214723..5962a42 100644 --- a/tests/auto/declarative/qdeclarativelistmodel/tst_qdeclarativelistmodel.cpp +++ b/tests/auto/declarative/qdeclarativelistmodel/tst_qdeclarativelistmodel.cpp @@ -337,7 +337,6 @@ void tst_QDeclarativeListModel::dynamic_worker() } delete item; - QTest::ignoreMessage(QtWarningMsg, "QThread: Destroyed while thread is still running"); qApp->processEvents(); } @@ -367,7 +366,6 @@ void tst_QDeclarativeListModel::convertNestedToFlat_fail() QCOMPARE(model.count(), 2); delete item; - QTest::ignoreMessage(QtWarningMsg, "QThread: Destroyed while thread is still running"); qApp->processEvents(); } @@ -427,7 +425,6 @@ void tst_QDeclarativeListModel::convertNestedToFlat_ok() QCOMPARE(model.count(), count+1); delete item; - QTest::ignoreMessage(QtWarningMsg, "QThread: Destroyed while thread is still running"); qApp->processEvents(); } -- cgit v0.12