Fix possible curl handle leak

--HG--
branch : develop
This commit is contained in:
Nimetu 2015-04-20 20:50:56 +03:00
parent 26571de439
commit a6cf9730be

View file

@ -192,10 +192,6 @@ namespace NLGUI
} }
} }
CURL *curl = curl_easy_init();
curl_easy_setopt(curl, CURLOPT_NOPROGRESS, true);
curl_easy_setopt(curl, CURLOPT_URL, finalUrl.c_str());
// use requested url for local name // use requested url for local name
string dest = localImageName(url); string dest = localImageName(url);
string tmpdest = localImageName(url)+".tmp"; string tmpdest = localImageName(url)+".tmp";
@ -209,13 +205,30 @@ namespace NLGUI
if (!NLMISC::CFile::fileExists(dest)) if (!NLMISC::CFile::fileExists(dest))
{ {
if (!MultiCurl)
{
nlwarning("Invalid MultiCurl handle, unable to download '%s'", finalUrl.c_str());
return;
}
CURL *curl = curl_easy_init();
if (!curl)
{
nlwarning("Creating cURL handle failed, unable to download '%s'", finalUrl.c_str());
return;
}
FILE *fp = fopen (tmpdest.c_str(), "wb"); FILE *fp = fopen (tmpdest.c_str(), "wb");
if (fp == NULL) if (fp == NULL)
{ {
curl_easy_cleanup(curl);
nlwarning("Can't open file '%s' for writing: code=%d '%s'", tmpdest.c_str (), errno, strerror(errno)); nlwarning("Can't open file '%s' for writing: code=%d '%s'", tmpdest.c_str (), errno, strerror(errno));
return; return;
} }
curl_easy_setopt(curl, CURLOPT_NOPROGRESS, true);
curl_easy_setopt(curl, CURLOPT_URL, finalUrl.c_str());
curl_easy_setopt(curl, CURLOPT_FILE, fp); curl_easy_setopt(curl, CURLOPT_FILE, fp);
curl_multi_add_handle(MultiCurl, curl); curl_multi_add_handle(MultiCurl, curl);
@ -298,19 +311,30 @@ namespace NLGUI
} }
if (action != "delete") if (action != "delete")
{ {
CURL *curl = curl_easy_init(); if (!MultiCurl)
if (!MultiCurl || !curl) {
nlwarning("Invalid MultiCurl handle, unable to download '%s'", url.c_str());
return false; return false;
}
curl_easy_setopt(curl, CURLOPT_NOPROGRESS, true); CURL *curl = curl_easy_init();
curl_easy_setopt(curl, CURLOPT_URL, url.c_str()); if (!curl)
{
nlwarning("Creating cURL handle failed, unable to download '%s'", url.c_str());
return false;
}
FILE *fp = fopen (tmpdest.c_str(), "wb"); FILE *fp = fopen (tmpdest.c_str(), "wb");
if (fp == NULL) if (fp == NULL)
{ {
curl_easy_cleanup(curl);
nlwarning("Can't open file '%s' for writing: code=%d '%s'", tmpdest.c_str (), errno, strerror(errno)); nlwarning("Can't open file '%s' for writing: code=%d '%s'", tmpdest.c_str (), errno, strerror(errno));
return false; return false;
} }
curl_easy_setopt(curl, CURLOPT_NOPROGRESS, true);
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_FILE, fp); curl_easy_setopt(curl, CURLOPT_FILE, fp);
curl_multi_add_handle(MultiCurl, curl); curl_multi_add_handle(MultiCurl, curl);