curl-library
Callback memory error
Date: Fri, 25 Apr 2003 19:43:33 +0530
Hi,
I'm using libcurl to write C++ wrappers over https functionality.
I'm using CallBack functions to get the server response and headers in
memory. I have a function with the code snipet as shown below:
...
switch (request_type)
{
case HTTPS_POST:
struct ServerResponse response;
response.szResponse = NULL;
response.size = 0;
struct Header header;
header.szHeader = NULL;
header.size = 0;
m_CurlReturnCode = curl_easy_setopt(m_pCurlHandle, CURLOPT_POST, TRUE);
m_CurlReturnCode = curl_easy_setopt(m_pCurlHandle, CURLOPT_POSTFIELDS,
szPostFields);
m_CurlReturnCode = curl_easy_setopt(m_pCurlHandle,
CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
m_CurlReturnCode = curl_easy_setopt(m_pCurlHandle, CURLOPT_FILE,
(void*)&response);
m_CurlReturnCode = curl_easy_setopt(m_pCurlHandle,
CURLOPT_HEADERFUNCTION, WriteMemoryCallback);
m_CurlReturnCode = curl_easy_setopt(m_pCurlHandle, CURLOPT_WRITEHEADER,
(void*)&header);
case HTTPS_GET: // Excuse the code duplication
struct ServerResponse get_response;
get_response.szResponse = NULL;
get_response.size = 0;
m_CurlReturnCode = curl_easy_setopt(m_pCurlHandle, CURLOPT_HTTPGET,
TRUE);
m_CurlReturnCode = curl_easy_setopt(m_pCurlHandle, CURLOPT_UPLOAD,
FALSE);
m_CurlReturnCode = curl_easy_setopt(m_pCurlHandle,
CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
m_CurlReturnCode = curl_easy_setopt(m_pCurlHandle, CURLOPT_FILE,
(void*)&get_response);
case HTTPS_PUT:
...
}
m_CurlReturnCode = curl_easy_perform(m_pCurlHandle);
...
The call back function is as below:
size_t WriteMemoryCallback(void* ptr, size_t size, size_t nmemb, void* data)
{
register int realsize = size * nmemb;
struct ServerResponse* mem = (struct ServerResponse*)data;
mem->szResponse = (char*)realloc(mem->szResponse, mem->size + realsize
+ 1);
if (mem->szResponse)
{
memcpy(&(mem->szResponse[mem->size]), ptr, realsize);
mem->size += realsize;
mem->szResponse[mem->size] = 0;
}
return realsize;
}
From my client application, But when I call Post followed by Get, it crashes
in the callback as the data becomes invalid and mem->size shows garbage
value(3435973836) . So it fails in realloc. But If I call Get and then Put
it works fine. (sounds insane...just writing what is happening and hoping
for some clue).
However if I comment out the code for reading the headers, it works fine for
both sequence.
I'm not able to explain clearly ( I can see that), but I feel that I'm
missing something trivial here. Any inputs will be highly appreciated.
Thanks and Regards,
Naved
-------------------------------------------------------
This sf.net email is sponsored by:ThinkGeek
Welcome to geek heaven.
http://thinkgeek.com/sf
Received on 2003-04-25