2017-11-29 11 views
0

、私は定期的に次のエラーを取得:libcurlの(関連セクションでは、以下の貼り付け)私のコードを実行している場合は、 "curl_easy_cleanup()" のmallocエラー(C++)を引き起こしている

program(34010,0x70000e58b000) malloc: *** error for object 0x7fc43d93fcf0: pointer being freed was not allocated set a breakpoint in malloc_error_break to debug Signal: SIGABRT (signal SIGABRT)

を私は、マルチスレッドC++のコードを実行しています異なるスレッドが問題のコードを同時に使用するMacbook(OS-10.13)上で実行することができます。私が知っている限り、libcurlは、2つの異なるスレッドで同じ「カール・ハンドル」(「CURL * curl = curl_easy_init();」のインスタンスであると理解している限り、スレッドセーフです。同時に。私の場合、各スレッドは関数を別々に呼び出し、CURLオブジェクトの新しいインスタンスを初期化するので、私は "安全"でなければなりません、そうですか?うまくいけば、既に解放されているメモリを解放しようとする私(またはこの場合libカール)を引き起こしている欠けている何かが明らかにある。それ以上の情報があれば(下記参照)、私に知らせてください。

障害をワンセグ機能は以下

res = curl_easy_perform(curl);

何を読み取っライン上

curl_easy_cleanup(curl);

、時には(より少ないことが多い)を読み取りライン上の

string http_lib::make_get_request(string url)

です私は自分のコードの適切なセクションになると思う:

メインで
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: %s\n", 
        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: %s\n", curl_easy_strerror(res)); 

     /* always cleanup */ 
     curl_easy_cleanup(curl); 
    } 

    if (networking_debug){ 
     cout << "Response: " << s << endl; 
    } 

    return s; 
} 

()私は/ /Applications/Xcode.app/Contents/Developer/Platforms/」のCURL_ROOT_DIR以外を使用したいしていないようでしたしません

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). 
} 
+1

コールバックの文字列の明示的なサイズ変更とコピーの代わりに、受信した文字列を[* append *](http://en.cppreference.com/w/cpp/string/basic_string/append)にするのはなぜですか? –

+0

そして、 'http_lib'とは何ですか?それは名前空間ですか?それはクラスですか?それがクラスの場合、 'CurlWrite_CallbackFunc_StdString'は静的メンバー関数ですか? –

+0

http_libは、関数が入っているhpp/cppファイルの名前です。クラスではありません。自分のプログラム内のネットワーク呼び出しに関連する関数のコンテナー/名前空間。なぜ私が追加しなかったのか、私は正当な理由がない。 – Xandrix

答えて

0

CMAKEを持っていますMacOSX.platform/Developer/SDKs/MacOSX10.13.sdk/usr/include "(libcurl(別名カール)用)

したがって、mac(および/またはXcode)が同梱されているcurl libを使用していました。私はどのバージョンがあるのか​​分かっていませんが、ではなくであり、代わりにCURLバージョン7.57を使用しているのはです。私の問題です。

私は作成した/usr/local/Cellar/curl/7.57.0ディレクトリをやっ

brew install curl 

に「醸造」パッケージマネージャを使用して、すべてのlibsを入れて/そこに含まれています。

は、その後、私は私のCMAKE CMAKE_CXX_FLAGSに

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

を追加しました。

TLDR;解決策は、私がcurl libの最新バージョンを使用していることを確認することでした。今、私は問題ありません。

関連する問題