curl und segfault
-
Tagchen,
ich hab folgendes:
ein Programm, das eine Internetseite in ein struct schreiben soll.
der eigentliche Code, der die URL entgegen nimmt und die Seite "ansurft" sowie die daten in das struct schreibt ist fast 1:1 eines der Beispiele zur libcurl, nur eben in eine Klasse gequetsch.
die klasse:#include "config.h" #include "http.h" #include "Version.h" #include <curl/curl.h> #include <curl/curlver.h> #include <iostream> #include <string> #include <cstring> using namespace std; void *HTTP::myrealloc(void *ptr, size_t size) { /* There might be a realloc() out there that doesn't like reallocing NULL pointers, so we take care of it here */ if (ptr) return realloc(ptr, size); else return malloc(size); } size_t HTTP::WriteMemoryCallback(void *ptr, size_t size, size_t nmemb, void *data) { size_t realsize = size * nmemb; struct MemoryStruct *mem =( struct MemoryStruct *)data; mem->memory = (char *)myrealloc(mem->memory, mem->size + realsize + 1); if (mem->memory) { memcpy(&(mem->memory[mem->size]), ptr, realsize); mem->size += realsize; mem->memory[mem->size] = 0; } return realsize; } HTTP::HTTP(const char * p_prj_url, const char * p_file, char * p_uid) { request_uri.append(p_prj_url); request_uri.append(p_file); request_uri.append(p_uid); cout << "prj_url: " << p_prj_url << endl << "p_file: " << p_file << endl << "p_uid: " << p_uid << endl << "request_uri: " << request_uri << endl; chunk.memory=NULL; /* we expect realloc(NULL, size) to work */ chunk.size = 0; /* no data at this point */ cout << "no data at this point" << endl; } HTTP::~HTTP() { /* cleanup curl stuff */ curl_easy_cleanup(curl_handle); if (chunk.memory) { free(chunk.memory); } } char * HTTP::get_code(void) { return chunk.memory; } size_t * HTTP::get_size(void) { return &chunk.size; } void HTTP::performRequest(void) { curl_global_init(CURL_GLOBAL_ALL); /* init the curl session */ curl_handle = curl_easy_init(); /* specify URL to get */ curl_easy_setopt(curl_handle, CURLOPT_URL, request_uri.c_str()); /* send all data to this function */ curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, &HTTP::WriteMemoryCallback); /* we pass our 'chunk' struct to the callback function */ curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, (void *)&chunk); /* some servers don't like requests that are made without a user-agent field, so we provide one */ cout << "setting user-agent: " << "boinc_data/" << BOINCDATA_VERSION << " using libcurl/" << LIBCURL_VERSION << endl; curl_easy_setopt(curl_handle, CURLOPT_USERAGENT, "boinc_data/"BOINCDATA_VERSION" using libcurl/"LIBCURL_VERSION); const char * encoding = "UTF-8"; cout << "setting encoding: " << encoding << endl; curl_easy_setopt(curl_handle, CURLOPT_ENCODING, encoding); curl_easy_perform(curl_handle); cout << "Data dump: " << chunk.memory << endl << "Size: " << chunk.size << endl; }alles was cout betrifft ist normalerweise nicht da, nur zum einfacheren debugging. Wenn ich das Programm mit den entsprechenden Parametern aufrufe, kommt das raus:
prj_url: http://setiweb.ssl.berkeley.edu/
p_file: show_user.php?userid=
p_uid: 1234
request_uri: http://setiweb.ssl.berkeley.edu/show_user.php?userid=1234
no data at this point
setting user-agent: boinc_data/0.0.1 using libcurl/7.16.2
setting encoding: UTF-8
Speicherzugriffsfehlermit gdb debugged:
prj: 28
uid: 1
mode: 0
file: show_user.php?userid=
instantiating the HTTP-Class...
prj_url: http://setiweb.ssl.berkeley.edu/
p_file: show_user.php?userid=
p_uid: 1
request_uri: http://setiweb.ssl.berkeley.edu/show_user.php?userid=1
no data at this point
setting user-agent: boinc_data/0.0.1 using libcurl/7.16.2
setting encoding: UTF-8Program received signal SIGSEGV, Segmentation fault.
0xb7d616f6 in realloc () from /lib/i686/cmov/libc.so.6wieso kommt hier ein segfault raus? ich gebe ihm per request_uri.c_str() ein char-array bzw. einen pointer drauf, oder? wenn ich den string direkt uebergebe bricht das programm naemlich ab, sobald ich chunk.memory ausgeben will, allerdings ohne irgendwelche meldungen, das endl kommt garnet mehr zum zuge...
ich muss das programm bis dienstag mittag fertig haben, sonst haette ich noch mehr herumprobiert.
wer weiss Rat?
C167
-
okay, kann geschlossen werden, ich hab myrealloc sowie WriteMemoryCallback aus der Klasse rausgenommen und als normale funktionen deklariert, wie dies auch im CURL-Beispiel aufgefuehrt ist... siehe da, schon geht es