changeset 833:3cc81dbc31bb

Use thread-local storage for network managers; otherwise we get "Cannot create children for a parent that is in a different thread" from nm.get()
author Chris Cannam
date Fri, 19 Jul 2013 13:47:11 +0100
parents 420ade1cb6da
children fcee7e040ab4
files data/fileio/FileSource.cpp
diffstat 1 files changed, 10 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/data/fileio/FileSource.cpp	Wed Jul 17 15:43:42 2013 +0100
+++ b/data/fileio/FileSource.cpp	Fri Jul 19 13:47:11 2013 +0100
@@ -25,6 +25,7 @@
 #include <QFileInfo>
 #include <QDir>
 #include <QCoreApplication>
+#include <QThreadStorage>
 
 #include <iostream>
 #include <cstdlib>
@@ -67,7 +68,7 @@
 }
 #endif
 
-static QNetworkAccessManager nm;
+static QThreadStorage<QNetworkAccessManager *> nms;
 
 FileSource::FileSource(QString fileOrUrl, ProgressReporter *reporter,
                        QString preferredContentType) :
@@ -260,6 +261,13 @@
 void
 FileSource::init()
 {
+    { // check we have a QNetworkAccessManager
+        QMutexLocker locker(&m_mapMutex);
+        if (!nms.hasLocalData()) {
+            nms.setLocalData(new QNetworkAccessManager());
+        }
+    }
+
     if (isResource()) {
 #ifdef DEBUG_FILE_SOURCE
         std::cerr << "FileSource::init: Is a resource" << std::endl;
@@ -443,7 +451,7 @@
              QString("%1, */*").arg(m_preferredContentType).toLatin1());
     }
 
-    m_reply = nm.get(req);
+    m_reply = nms.localData()->get(req);
 
     connect(m_reply, SIGNAL(readyRead()),
             this, SLOT(readyRead()));