From 1eb02f83a5d3895e4e18db30ea10d88da94ba4c0 Mon Sep 17 00:00:00 2001 From: Andrei Karas Date: Sat, 5 Dec 2009 02:33:52 +0200 Subject: Fixing multi thread crash. --- src/gui/serverdialog.cpp | 4 ++++ src/gui/updatewindow.cpp | 4 ++++ src/net/download.cpp | 31 +++++++++++++++++++++++++++---- 3 files changed, 35 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/gui/serverdialog.cpp b/src/gui/serverdialog.cpp index 9c1494d0..ec2b0cf4 100644 --- a/src/gui/serverdialog.cpp +++ b/src/gui/serverdialog.cpp @@ -171,6 +171,10 @@ ServerDialog::ServerDialog(ServerInfo *serverInfo, const std::string &dir): ServerDialog::~ServerDialog() { + if (mDownload) + { + mDownload->cancel(); + } delete mServersListModel; } diff --git a/src/gui/updatewindow.cpp b/src/gui/updatewindow.cpp index 14ebe6b1..801bd161 100644 --- a/src/gui/updatewindow.cpp +++ b/src/gui/updatewindow.cpp @@ -115,6 +115,10 @@ UpdaterWindow::UpdaterWindow(const std::string &updateHost, UpdaterWindow::~UpdaterWindow() { + if (mDownload) + { + mDownload->cancel(); + } free(mMemoryBuffer); } diff --git a/src/net/download.cpp b/src/net/download.cpp index 2d626270..51034e0c 100644 --- a/src/net/download.cpp +++ b/src/net/download.cpp @@ -136,7 +136,13 @@ bool Download::start() void Download::cancel() { logger->log("Canceling download: %s\n", mUrl.c_str()); + mOptions.cancel = true; + if (mThread && SDL_GetThreadID(mThread) != 0) + { + SDL_WaitThread(mThread, NULL); + mThread = NULL; + } } char *Download::getError() @@ -167,20 +173,30 @@ int Download::downloadThread(void *ptr) CURLcode res; std::string outFilename; + if (!d) + { + return 0; + } if (!d->mOptions.memoryWrite) { outFilename = d->mFileName + ".part"; } - while (attempts < 3 && !complete) + while (attempts < 3 && !complete && !d->mOptions.cancel) { FILE *file = NULL; d->mUpdateFunction(d->mPtr, DOWNLOAD_STATUS_STARTING, 0, 0); + if (d->mOptions.cancel) + { + d->mThread = NULL; + return 0; + } + d->mCurl = curl_easy_init(); - if (d->mCurl) + if (d->mCurl && !d->mOptions.cancel) { logger->log("Downloading: %s", d->mUrl.c_str()); @@ -211,7 +227,7 @@ int Download::downloadThread(void *ptr) curl_easy_setopt(d->mCurl, CURLOPT_NOSIGNAL, 1); curl_easy_setopt(d->mCurl, CURLOPT_CONNECTTIMEOUT, 15); - if ((res = curl_easy_perform(d->mCurl)) != 0) + if ((res = curl_easy_perform(d->mCurl)) != 0 && !d->mOptions.cancel) { switch (res) { @@ -285,6 +301,11 @@ int Download::downloadThread(void *ptr) complete = true; } } + if (d->mOptions.cancel) + { + d->mThread = NULL; + return 0; + } attempts++; } @@ -292,7 +313,8 @@ int Download::downloadThread(void *ptr) { // Nothing to do... } - else if (!complete || attempts >= 3) { + else if (!complete || attempts >= 3) + { d->mUpdateFunction(d->mPtr, DOWNLOAD_STATUS_ERROR, 0, 0); } else @@ -300,6 +322,7 @@ int Download::downloadThread(void *ptr) d->mUpdateFunction(d->mPtr, DOWNLOAD_STATUS_COMPLETE, 0, 0); } + d->mThread = NULL; return 0; } -- cgit v1.2.3-70-g09d2