libcurl program crashes on curl_easy_init?
I wrote a simple libcurl program with several calls to the following function:
Code:
char* import(char* in) {
CURL *curl;
puts("*curl");
curl = curl_easy_init();
puts("init");
if(curl == NULL)
{
puts("EASY_INIT WENT WRONG.");
exit(0);
}
char *data;
/* Create the write buffer */
data = malloc(1024 * 256);
if (data == NULL)
{
puts("Not enough memory for import buffer.");
exit(0);
}
struct write_result write_result = {
.data = data,
.pos = 0
};
/* Set curl's parameters */
curl_easy_setopt(curl, CURLOPT_URL,in);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curl_write);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &write_result);
curl_easy_perform(curl);
/* null terminate the string */
data[write_result.pos] = '\0';
curl_easy_cleanup(curl);
curl_global_cleanup();
return(data);
}
The weird thing is, I use the function often in the main program, and sometimes it works, sometimes it doesn't. When it doesn't work, it gets as far as printing "*curl", and then just crashes. Generally what happens is, it crashes several times in a row, on the exact same call (the previous work perfectly), and then inexplicably works fine again later on...
Does anyone have any idea what may be causing this and how to stop it???
Any help would be greatly appreciated