libcurl curl_easy_perform崩溃(分段错误)c ++

时间:2017-07-14 08:05:12

标签: c++ curl crash libcurl

我很抱歉我的英语不好。 我正在尝试运行以下代码但是当进度运行大约一天或几个小时它崩溃,所以这次崩溃是偶然发生的。 顺便说一句,SecMonitor_Curl是一个单独的类,所以curl_global_init()只运行一次全局。 我无法解决这个问题,所以我不知道我的程序有什么问题。请帮我。谢谢!

   SecMonitor_Curl::SecMonitor_Curl()
{
    curl_global_init(CURL_GLOBAL_ALL);
}

SecMonitor_Curl::~SecMonitor_Curl()
{
    curl_global_cleanup();
}


static size_t write_to_string(void *ptr, size_t size, size_t nmemb, void *stream) {
    ((std::string*)stream)->append((char*)ptr, 0, size*nmemb);
    return size * nmemb;
}


int SecMonitor_Curl::http_get(const std::string url, const std::string method, const std::map<std::string, std::string>& params, std::string post_data, std::string& response)
{
    int ret = 0;
    CURLcode res;
    curl_ = curl_easy_init();
    if (curl_ == NULL)
    {
        return -1;
    }
    if(curl_)
    {
        url_t = "www.google.com";
        method = "POST";
        post_body="{"test":"test"}";

        res = curl_easy_setopt(curl_, CURLOPT_URL, url_t.c_str());

        if (method == "POST" || method == "PUT" || method == "DELETE")
        {
            curl_easy_setopt(curl_, CURLOPT_CUSTOMREQUEST, method.c_str());
            curl_easy_setopt(curl_, CURLOPT_POSTFIELDS, post_body.c_str());
            curl_easy_setopt(curl_, CURLOPT_POSTFIELDSIZE, post_body.size());
        }

        res = curl_easy_setopt(curl_, CURLOPT_FOLLOWLOCATION, 1L);
        res = curl_easy_setopt(curl_, CURLOPT_NOSIGNAL, 1L); 
        res = curl_easy_setopt(curl_, CURLOPT_ACCEPT_ENCODING, "deflate");
        std::string out;
        res = curl_easy_setopt(curl_, CURLOPT_WRITEFUNCTION, write_to_string);
        res = curl_easy_setopt(curl_, CURLOPT_WRITEDATA, &out);

        //printf("curl_version : %s ",curl_version());
        res = curl_easy_perform(curl_);
        /* Check for errors */
        if (res != CURLE_OK) {
            srlog_error("SecMonitor_Curl | curl_easy_perform() failed: %s\n",
                    curl_easy_strerror(res));
            ret = -1;
        }
        response = out;
    }
    else
    {
        ret = -1;
    }
    curl_easy_cleanup(curl_);

    return ret;
}

这是转储文件:

Program terminated with signal 11, Segmentation fault.
#0  _IO_fwrite (buf=0x7f16a31dba70, size=2, count=1, fp=0x0) at iofwrite.c:43
43  iofwrite.c: No such file or directory.
    in iofwrite.c    
(gdb) bt
#0  _IO_fwrite (buf=0x7f16a31dba70, size=2, count=1, fp=0x0) at iofwrite.c:43
#1  0x00007f16a31aef93 in ?? () from /usr/lib64/libcurl.so.4
#2  0x00007f16a31af0c0 in Curl_debug () from /usr/lib64/libcurl.so.4
#3  0x00007f16a31afd69 in Curl_infof () from /usr/lib64/libcurl.so.4
#4  0x00007f16a31b55f4 in Curl_protocol_connect () from /usr/lib64/libcurl.so.4
#5  0x00007f16a31bbb0c in Curl_connect () from /usr/lib64/libcurl.so.4
#6  0x00007f16a31c3a90 in Curl_perform () from /usr/lib64/libcurl.so.4
#7  0x0000000000437a10 in SecMonitor_Curl::http_get (this=0x11e2db8, url=
    "http://dip.alibaba-inc.com/api/v2/services/schema/mock/61919?spm=a312q.7764190.0.0.40e80cf75fUogt", method="POST", params=std::map with 5 elements = {...}, post_data="", response="")
    at /home/albert.yb/secMonitorAgent/secMonitor/monitor/server/SecMonitor/SecMonitor_Curl.cpp:131
#8  0x0000000000435ab0 in SecMonitor_Cmd::run_cmd (this=0x11eeef8, cmd_name="update")

SecMonitor_Curl.cpp:131:表示curl_easy_perform()。

由于

1 个答案:

答案 0 :(得分:1)

我收集了3个write_to_string回调函数: 第一:

static size_t write_to_string(void *ptr, size_t size, size_t nmemb, void *stream) {
    ((std::string*)stream)->append((const char*)ptr, size*nmemb);
    return size * nmemb;
}

第二:

static size_t write_to_string(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;
}

第三

   struct MemoryStruct {
      char *memory;
      size_t size;
    };

    static size_t
    write_to_string(void *contents, size_t size, size_t nmemb, void *userp)
    {
      size_t realsize = size * nmemb;
      struct MemoryStruct *mem = (struct MemoryStruct *)userp;

      mem->memory = realloc(mem->memory, mem->size + realsize + 1);
      if(mem->memory == NULL) {
        /* out of memory! */
        printf("not enough memory (realloc returned NULL)\n");
        return 0;
      }

      memcpy(&(mem->memory[mem->size]), contents, realsize);
      mem->size += realsize;
      mem->memory[mem->size] = 0;

      return realsize;
    }

这些回调方法可以处理这个输出curl响应String的事情。但这个问题在这里没有,所有崩溃都是因为&#34; crul _&#34;这是该类的成员变量。当&#34; http_get&#34;的功能时由多线程引用,必须发生崩溃。

相关问题