库"curl_easy_cleanup()"导致 malloc 错误 (C++)



运行我的代码时(下面粘贴的相关部分),我定期收到以下错误:

程序(34010,0x70000E58B000)malloc:***对象错误 0x7FC43D93FCF0:未分配的指针设置一个断点 在malloc_error_break to debug信号中:sigabrt(信号sigabrt)

我正在MacBook(OS-10.13)上运行多线程C 代码,其中不同的线程同时使用有关代码。据我所知,只要我不利用相同的" curl handing"(我理解是" curl" aka" aka" curl *curl *curl = curl_easy_init()),libcurl确实是安全的。同时。就我而言,由于每个线程单独调用函数并初始化了卷曲对象的新实例,所以我应该"安全",对吗?希望有一些很明显的是,我缺少的是导致我(或在这种情况下lib curl)试图释放已经被释放的记忆。如果还有更多信息,我应该包括(下面),请不要犹豫,让我知道。

SEG故障的功能

字符串http_lib :: make_get_request(string url)

在读取

的线上

curl_easy_cleanup(curl);

有时(较少频率)在读取

的行上

res = curl_easy_perforf(curl);

以下是我认为代码的相关部分:

size_t http_lib::CurlWrite_CallbackFunc_StdString(void *contents, size_t size, size_t nmemb, std::string *s)
{
    size_t newLength = size*nmemb;
    size_t oldLength = s->size();
    try
    {
        s->resize(oldLength + newLength);
    }
    catch(std::bad_alloc &e)
    {
        //handle memory problem
        return 0;
    }
    std::copy((char*)contents,(char*)contents+newLength,s->begin()+oldLength);
    return size*nmemb;
}
string http_lib::make_post_request(string url, vector<string> headers, string post_params) {
    CURL *curl;
    CURLcode res;
    curl = curl_easy_init();
    string s;
    if(curl)
    {
        struct curl_slist *chunk = NULL;
        for(int i=0; i<headers.size(); i++){
            /* Add a custom header */
            chunk = curl_slist_append(chunk, headers[i].c_str());
        }
        /* set our custom set of headers */
        res = curl_easy_setopt(curl, CURLOPT_HTTPHEADER, chunk);
        curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
        curl_easy_setopt(curl, CURLOPT_POSTFIELDS, post_params.c_str());
        curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L); //only for https
        curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L); //only for https
        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, CurlWrite_CallbackFunc_StdString);
        curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s);
        if(networking_debug){
            curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L); //verbose output
        }
        /* Perform the request, res will get the return code */
        res = curl_easy_perform(curl);
        /* Check for errors */
        if(res != CURLE_OK)
        {
            fprintf(stderr, "curl_easy_perform() failed: %sn",
                    curl_easy_strerror(res));
        }
        /* always cleanup */
        curl_easy_cleanup(curl);
    }
    // Debug output
    if (networking_debug){
        cout<<"Response: " << s <<endl;
    }
    return s;
}
string http_lib::make_get_request(string url) {
    //SslCurlWrapper sslObject;
    CURL *curl;
    CURLcode res;
    curl = curl_easy_init();
    string s;
    if (curl) {
        curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
        //tell libcurl to follow redirection
        curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
        curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L); //only for https
        curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L); //only for https
        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, CurlWrite_CallbackFunc_StdString);
        curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s);
        if(networking_debug){
            curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L); //verbose output
        }
        /* Perform the request, res will get the return code */
        res = curl_easy_perform(curl);
        /* Check for errors */
        if (res != CURLE_OK)
            fprintf(stderr, "curl_easy_perform() failed: %sn", curl_easy_strerror(res));
        /* always cleanup */
        curl_easy_cleanup(curl);
    }
    if (networking_debug){
        cout << "Response: " << s << endl;
    }
    return s;
}

在main()中我有

int main(int argc, char *argv[]){
    // Initialize http_lib (curl)
    curl_global_init(CURL_GLOBAL_DEFAULT);
    ... spin up 10 or so threads that make get/post requests to https site (some requests utilize the make_post_request() function and others utilize make_get_requet() function). 
}

cmake cmake不/似乎不想使用"/applications/xcode.app.app/contents/contents/developer/developer/platforms/macosx.platforms/macosx.platform/developer/developer/sdks"以外的其他任何内容/macosx10.13.sdk/usr/include"用于libcurl(又名卷发)。

因此,它使用了与Mac(和/或Xcode)一起运输的卷曲lib。我还没有弄清楚它是什么版本,但是我可以说不是使用它,而是使用Curl版本7.57是 filex 我的问题。

我将"酿造"软件包管理器用于

brew install curl

这样做创建/usr/local/cellar/curl/7.57.0目录,并将所有libs/inclation放在其中。

然后我添加了

 -I/usr/local/Cellar/curl/7.57.0/include -L/usr/local/Cellar/curl/7.57.0/lib

到我的cmake cmake_cxx_flags。

tldr;解决方案是确保我使用的是最新版本的卷曲lib。现在我是没问题。

相关内容

  • 没有找到相关文章

最新更新