commit 001145c1a72312e4be08f0a2a6b35b89bd8ee3a6 Author: Aaron McCarthy Date: Wed Aug 25 16:51:07 2010 +1000 Fix thread cleanup. deleteLater() was being called from within the thread, which caused it to be deleted in the next iteration of the event loop in the main thread, possibly before it had finished. diff --git a/DownloadThread.cpp b/DownloadThread.cpp index c6ebdb2..8be5da7 100644 --- a/DownloadThread.cpp +++ b/DownloadThread.cpp @@ -40,7 +40,6 @@ void DownloadThread::run() exec(); emit downloadCompleted(this); - deleteLater(); delete m_NetworkAccessManager; } diff --git a/SocketThread.cpp b/SocketThread.cpp index 93278b2..09dca23 100644 --- a/SocketThread.cpp +++ b/SocketThread.cpp @@ -15,11 +15,6 @@ SocketThread::SocketThread(QObject *inparent) : SocketThread::~SocketThread() { - if (s != NULL) - { - s->close(); - delete s; - } } void SocketThread::StartConnection() @@ -71,6 +66,10 @@ void SocketThread::run() QString host = "samplehost.com"; // Note: please use a server host name which is supporting SSL, and which can bear silent client. s->connectToHost(host, 443); exec(); + + s->close(); + delete s; + s = 0; } void SocketThread::encryptedBytesWritten(qint64 nBytes) diff --git a/sockettest.cpp b/sockettest.cpp index ec62f46..5b1234b 100644 --- a/sockettest.cpp +++ b/sockettest.cpp @@ -8,13 +8,15 @@ #include CSocketTest::CSocketTest(QObject *inparent) : - QObject(inparent) + QObject(inparent), expectedDownloads(0) { } CSocketTest::~CSocketTest() { + m_SocketThread->quit(); + m_SocketThread->wait(); ASSERT(!m_SocketThread->isRunning()); delete m_SocketThread; } @@ -40,17 +42,23 @@ void CSocketTest::OnDownloadCompleted(DownloadThread *downloadThread) Q_UNUSED(downloadThread); qWarning() << "one download completed"; //we expect to complete all downloads after download within 2 mins. Not an elegant way to exit, but should do for test app. - QTimer::singleShot(120000, qApp, SLOT(quit())); + if (--expectedDownloads == 0) + QTimer::singleShot(120000, qApp, SLOT(quit())); } void CSocketTest::TestDownloadFile() { + ++expectedDownloads; + DownloadThread* downloadThread; qWarning() << "Launching one download"; downloadThread = new DownloadThread(this); ASSERT(!downloadThread->isRunning()); QObject::connect(downloadThread, SIGNAL(downloadCompleted(DownloadThread*)), this, SLOT(OnDownloadCompleted(DownloadThread*))); + // delete download thread after it is finished. + QObject::connect(downloadThread, SIGNAL(finished()), downloadThread, SLOT(deleteLater())); + downloadThread->StartDownload(); //return S_OK; diff --git a/sockettest.h b/sockettest.h index 26add51..192ade8 100644 --- a/sockettest.h +++ b/sockettest.h @@ -34,5 +34,6 @@ public: private: QPointer m_SocketThread; void TestDownloadFile(); + int expectedDownloads; }; #endif /* SOCKETTEST_H_ */