curl-library
RE: CURLOPT_WRITEFUNCTION - cannot make it working...
Date: Sat, 4 Jun 2011 16:31:28 -0700
>> With the *exact* source code used in getinmemory.c or after you've C++ified it
to your liking?
Yes, the *EXACT* getinmemory.c. I've put together a simplified test case here below that exhibits the same segmentation-fault.
It's self-contained:
=============================================
#include <curl/curl.h>
#include <curl/easy.h>
#include <iostream>
#include <fstream>
#include <cstring>
#include <stdlib.h>
#include <assert.h>
using namespace std;
struct MemoryStruct {
char *memory;
size_t size;
} ;
static size_t
WriteMemoryCallback(void *ptr, size_t size, size_t nmemb, void *data)
{
size_t realsize = size * nmemb;
struct MemoryStruct *mem = (struct MemoryStruct *)data;
// For Debugging
cout << (void *)(mem->memory) << " =mem->memory\t\t"
<< mem->size << " =mem->size\t"
<< (mem->size+realsize+1) << " =mem->size+realsize+1\n";
mem->memory = (char *)realloc(mem->memory, mem->size + realsize + 1);
// For Debugging
cout << "realloc: done!\n";
if (mem->memory == NULL) {
/* out of memory! */
printf("not enough memory (realloc returned NULL)\n");
exit(EXIT_FAILURE);
}
memcpy(&(mem->memory[mem->size]), ptr, realsize);
mem->size += realsize;
mem->memory[mem->size] = 0;
return realsize;
}
int main(void)
{
CURL *sessionA;
CURLcode result;
FILE *NULLFILE, *CookieFile, *HeaderFile, *WpageFile;
const char *CookiesBag = "Cookies.txt";
CookieFile = fopen(CookiesBag,"w");
HeaderFile = fopen("Header.html","w");
WpageFile = fopen("Wpage.html", "w");
NULLFILE = fopen("/dev/null", "w");
char *curlv;
curlv = curl_version();
cout << curlv << " (curl_version)\n\n";
curl_global_init(CURL_GLOBAL_ALL);
sessionA = curl_easy_init();
if(sessionA) {
// PREPARATION & KickOff
//
curl_easy_setopt(sessionA, CURLOPT_VERBOSE, 0);
curl_easy_setopt(sessionA, CURLOPT_FOLLOWLOCATION, 1);
curl_easy_setopt(sessionA, CURLOPT_COOKIELIST, "ALL"); //erase "ALL" cookies
curl_easy_setopt(sessionA, CURLOPT_COOKIEJAR, CookiesBag);
curl_easy_setopt(sessionA, CURLOPT_COOKIEFILE, CookiesBag);
curl_easy_setopt(sessionA, CURLOPT_WRITEDATA, NULLFILE);
curl_easy_setopt(sessionA, CURLOPT_WRITEHEADER, HeaderFile);
// from getinmemory.c
//
struct MemoryStruct chunk;
chunk.memory = (char *)malloc(1); /* will be grown as needed by the realloc above */
chunk.size = 0; /* no data at this point */
// Underlying Snapshot
//
// curl_easy_setopt(sessionA, CURLOPT_WRITEDATA, WpageFile);
curl_easy_setopt(sessionA, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
curl_easy_setopt(sessionA, CURLOPT_WRITEDATA, (void *)&chunk);
curl_easy_setopt(sessionA, CURLOPT_URL, "http://www.yahoo.com/");
// For Debugging
cout << (void *)chunk.memory << " =chunk.memory\t\t"
<< chunk.size << " =chunk.size\n";
result = curl_easy_perform(sessionA);
curl_easy_cleanup(sessionA);
}
return 0;
}
-------------------------------------------------------------------
List admin: http://cool.haxx.se/list/listinfo/curl-library
Etiquette: http://curl.haxx.se/mail/etiquette.html
Received on 2011-06-05