[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-commits
Subject:    [ktp-common-internals/kde-telepathy-0.6] KTp: Don't delete LogsImporter thread while it's still runn
From:       Dan_Vrátil <dvratil () redhat ! com>
Date:       2013-08-22 12:44:33
Message-ID: E1VCUFd-0000eE-Sa () scm ! kde ! org
[Download RAW message or body]

Git commit fb28de93e26b6c24a9667a16b21259e1737b3d25 by Dan Vrátil.
Committed on 22/08/2013 at 12:43.
Pushed by dvratil into branch 'kde-telepathy-0.6'.

Don't delete LogsImporter thread while it's still running

When LogsImporter is deleted, it just deletes the importer thread
even when it's running, which will obviously crash. This patch adds
a safety guard (calling QThread::wait() before deleting it) and a
method to stop the thread prematurely so that we don't wait() for
the entire import to finish.

REVIEW: 112188

M  +17   -3    KTp/logs-importer-private.cpp
M  +3    -0    KTp/logs-importer-private.h
M  +6    -1    KTp/logs-importer.cpp

http://commits.kde.org/telepathy-common-internals/fb28de93e26b6c24a9667a16b21259e1737b3d25

diff --git a/KTp/logs-importer-private.cpp b/KTp/logs-importer-private.cpp
index 7275b07..4def0c8 100644
--- a/KTp/logs-importer-private.cpp
+++ b/KTp/logs-importer-private.cpp
@@ -31,13 +31,18 @@ LogsImporter::Private::Private(KTp::LogsImporter* parent)
   , m_month(0)
   , m_year(0)
   , m_isMUCLog(false)
+  , m_shouldStop(0)
 {
 
 }
 
 LogsImporter::Private::~Private()
 {
+}
 
+void LogsImporter::Private::stop()
+{
+    m_shouldStop = 1;
 }
 
 void LogsImporter::Private::setAccountId(const QString& accountId)
@@ -55,6 +60,10 @@ void LogsImporter::Private::run()
 
     Q_FOREACH (const QString &file, files) {
         convertKopeteLog(file);
+
+        if (m_shouldStop == 1) {
+            return;
+        }
     }
 }
 
@@ -362,11 +371,16 @@ void LogsImporter::Private::convertKopeteLog(const QString& filepath)
     }
 
     for (int i = 0; i < kopeteMessages.count(); i++) {
-        QDomElement kopeteMessage = kopeteMessages.item(i).toElement();
+        const QDomElement kopeteMessage = kopeteMessages.item(i).toElement();
+        const QDomElement ktpMessage = convertKopeteMessage(kopeteMessage);
 
-        QDomElement ktpMessage = convertKopeteMessage(kopeteMessage);
+        if (!ktpMessage.isNull()) {
+            m_ktpLogElement.appendChild(ktpMessage);
+        }
 
-        m_ktpLogElement.appendChild(ktpMessage);
+        if (m_shouldStop == 1) {
+            return;
+        }
     }
 
     saveKTpDocument();
diff --git a/KTp/logs-importer-private.h b/KTp/logs-importer-private.h
index 06c9c0d..6ca7355 100644
--- a/KTp/logs-importer-private.h
+++ b/KTp/logs-importer-private.h
@@ -39,6 +39,8 @@ class LogsImporter::Private: public QThread
     void setAccountId(const QString &accountId);
     QStringList findKopeteLogs(const QString &accountId) const;
 
+    void stop();
+
   Q_SIGNALS:
     void error(const QString &error);
 
@@ -69,6 +71,7 @@ class LogsImporter::Private: public QThread
     int m_year;
 
     bool m_isMUCLog;
+    QAtomicInt m_shouldStop;
 
     friend class KTp::LogsImporter;
 };
diff --git a/KTp/logs-importer.cpp b/KTp/logs-importer.cpp
index 926d265..fd4996e 100644
--- a/KTp/logs-importer.cpp
+++ b/KTp/logs-importer.cpp
@@ -33,7 +33,12 @@ LogsImporter::LogsImporter(QObject *parent)
 
 LogsImporter::~LogsImporter()
 {
-    delete d;
+    if (d->isRunning()) {
+        d->stop();
+        d->wait();
+    }
+
+    // d is deleted by QObject
 }
 
 bool LogsImporter::hasKopeteLogs(const Tp::AccountPtr& account)
[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic