Curl和OpenSSL的内存泄漏

时间:2014-04-23 19:43:23

标签: c curl memory-leaks openssl

使用Curl(7.33.0)和OpenSSL(1.0.1e)时,我的内存泄漏严重。我很容易达到2GB的内存。在我检查的每台机器上重现,Win 7,Win8。

这是我正在开发的多线程应用程序的一部分。

myRetCode UploadFile(nschar_t *pPath, nschar_t *local_path)
{
    CURL        *curl = NULL;
    CURLcode    res = CURLE_OK;
    nsuint_t    http_code = 0;
    nschar_t    url[URL_LENGTH];
    nschar_t    errMsg[ERROR_SIZE];
    myRetCode   rc = OK_SUCCESS;
    curl_off_t  fsize;
    FILE        *fp;
    struct stat file_info;

    sprintf_s(url, sizeof(url), "https://mysite?",  pPath);

    fp = fopen(local_path, "rb");
    if(!fp)
    {
        return NS_FAILURE;
    }

    /* to get the file size */
    if(fstat(fileno(fp), &file_info) != 0)
    {
        return NS_FAILURE; /* can't continue */
    }
    fsize = (curl_off_t)stFileInfo.ulFileSize;

    curl = curl_easy_init();
    curl_easy_setopt(curl, CURLOPT_URL, url);
    curl_easy_setopt(curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
    curl_easy_setopt(curl, CURLOPT_USERPWD, "xyz");
    curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
    curl_easy_setopt(curl, CURLOPT_VERBOSE, 0L);
    curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 1L);
    curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1L);
    curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, errMsg);

    curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
    curl_easy_setopt(curl, CURLOPT_READFUNCTION, UploadFile_Data);
    curl_easy_setopt(curl, CURLOPT_READDATA, fp);
    curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE, (curl_off_t)file_info.st_size);
    curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "PUT");

    res = curl_easy_perform(curl);
    if(res != CURLE_OK)
    {
        curl_easy_cleanup(curl);
        fclose(fp);
        return MY_CURL_LIB_ERROR;
    }

    curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_code);
    curl_easy_cleanup(curl);
    RC = SUCCESS;

    fclose(fp);
    return rc;
}

size_t UploadFile_Data(char *ptr, size_t size, size_t nmemb, void *userp)
{
    size_t read;
    read = fread(ptr, size, nmemb, userp);
    return read;
}

我已使用_CrtDumpMemoryLeaks()完成内存调试。它显示了泄漏:

res = curl_easy_perform(curl);
curl = curl_easy_init();

以及任何有CURL调用的内容。

我已经阅读了许多帖子,其中讨论了使用OpenSSL和Curl时可能存在的内存泄漏,但没有明确的答案。难道我做错了什么?

0 个答案:

没有答案