Unsolved The inferior stopped because it received a signal from the operating system. Signal name : SIGSEGV Signal meaning : Segmentation fault
-
Hi All,
i am using curl with QT for downloading file using url. i am getting bellow error.
The inferior stopped because it received a signal from the operating system.
Signal name :
SIGSEGV
Signal meaning :
Segmentation faultcode is simple.
CURL *curl; FILE *fp; CURLcode res;
char str[]="http://guest:p2guest@192.168.55.3/contents/2/clip/0711WH.xml";
char * url=new char[255];
memset(url,'\0',sizeof(url));
memcpy(url,str,sizeof(str));
char outfilename[FILENAME_MAX] = "home/roi/Desktop/page.html";
curl = curl_easy_init();
if (curl)
{
fp = fopen(outfilename,"wb");
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, NULL);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
fclose(fp);
}
return 0;}
while debugging i am getting these error when controll comes to line
res = curl_easy_perform(curl);
please suggest me how to resolve it.
Thanks
Shashi -
@shashi-prasad First thing I see is that these lines don't do what you think they do:
memset(url,'\0',sizeof(url)); memcpy(url,str,sizeof(str));
With that
sizeof(url)
you are only getting the sizeof achar *
on your system which is 1. You aren't getting the "255" you are expecting. So when you call memcpy() you're not necessarily getting a null terminatedurl
string.That is probably where your crash is. Throw it in a debugger and get a backtrace and it will show you where you're crashing.
You're also leaking memory as you never clean up
url
.And finally, what's the point of setting
char str[]
and then memcpy it to another stringurl
? As far as I can see that is the only place you usestr
. So you don't need a variable for it. And if you insist on one, you should use aconst char *str = "whatever"
.And finally, the code you posted has absolutely no Qt in it. So you're in the wrong place to ask for help. This should be asked on a curl support forum. ;)
-
@shashi-prasad said in The inferior stopped because it received a signal from the operating system. Signal name : SIGSEGV Signal meaning : Segmentation fault:
char * url=new char[255];
memset(url,'\0',sizeof(url));
memcpy(url,str,sizeof(str));Does Curl take ownership of the provided URL (i.e. it frees the memory after usage)? If not, simply do the following:
char url[256]; url[255] = '\0'; memcpy(url, str, 255);
If you have a local array which's size can be determined at compile-time, sizeof(your_array) will also yield the correct result. But if you instead use sizeof(your_array) with a dynamically allocated array which's size is variable, sizeof(char*) will return 1 on most systems. That's why I suspect your problem does not lie in that piece of code.
After searching through the documentation of libcurl, I found the following (https://curl.haxx.se/libcurl/c/CURLOPT_WRITEDATA.html):
"If you're using libcurl as a win32 DLL, you MUST use the CURLOPT_WRITEFUNCTION if you set this option or you will experience crashes."
Your call:
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, NULL);
As you are passing a nullptr to this function, I highly suspect that this is the cause of the crash, since the function "curl_easy_perform" makes use of the callback you are required to register there.
I hope I could help you!