Reputation: 41
I am sorry for my bad English. I am trying to run the following code but it crashes when the progress run about one day or several hours, so this crash is come up by accident. By the way , The SecMonitor_Curl is a single Class, so curl_global_init() run only one time global. I can not resolve this question, so I don't know what wrong with my program . Please help me. thanks!
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;
}
This is the dump file :
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 : means curl_easy_perform().
Thanks
Upvotes: 1
Views: 2974
Reputation: 41
I collected 3 write_to_string callback function: First :
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;
}
Second:
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;
}
Third:
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;
}
These callback method can deal with this thing of output the curl response to String . But this problem is no in here, all crash because "crul_" which is the member variable of this class. when the function of "http_get" is quoting by multi thread , the crash must be happend.
Upvotes: 2