curl-library
Memory leaks using callbacks
Date: Mon, 12 Nov 2007 15:48:51 -0000
Hello.
I'm using libcurl to post a message using http in one url and I'm using
a callback to receive the answer.
My application has memory leaks, and I'm having a lot of difficulties to
know why. I'm using libcurl 7.12.1 for RedHat.
Here is the method that I have created to make the post:
int
CWebClient :: httpPost(string &url, string &postData, string &answer)
{
int pos, len;
string command;
CURLcode res;
char* resp;
CURL* httpHandle;
answer = "";
// Init httpHandle
httpHandle=curl_easy_init();
// Set the options for transfer
curl_easy_setopt (httpHandle, CURLOPT_URL, url.c_str ());
curl_easy_setopt (httpHandle, CURLOPT_POST, 1);
curl_easy_setopt (httpHandle, CURLOPT_POSTFIELDS, postData.c_str());
curl_easy_setopt (httpHandle, CURLOPT_NOSIGNAL, 1);
curl_easy_setopt (httpHandle, CURLOPT_TIMEOUT, 4);
curl_easy_setopt (httpHandle, CURLOPT_CONNECTTIMEOUT, 4);
if(progress)
curl_easy_setopt(httpHandle, CURLOPT_NOPROGRESS,false);
curl_easy_setopt(httpHandle, CURLOPT_WRITEFUNCTION,
&CWebClient::HTTPWriteCallback);
curl_easy_setopt(httpHandle, CURLOPT_WRITEDATA, &resp);
// Start the transfer
res = curl_easy_perform (httpHandle);
if( CURLE_OK != res)
{
// Cleanup
curl_easy_cleanup(httpHandle);
return res;
}
for( unsigned int i = 0; i < strlen(resp); i++ )
answer += resp[i];
answer += (char)0x00;
delete []resp;
// Cleanup
curl_easy_cleanup(httpHandle);
return SENTOK;
}
and here is the callback that I'm using:
size_t
CWebClient::HTTPWriteCallback(void *ptr, size_t size, size_t memb, char*
&data)
{
int nRealsize = size* memb;
char* aux = (char*)ptr;
//buffer data from ptr to data
data = new char[nRealsize+1];
for( unsigned int i = 0; i < nRealsize; i++ )
data[i] = (char)aux[i];
data[nRealsize] = (char)0x00;
return nRealsize;
}
Is there something missing? I'm doing the
curl_global_init (CURL_GLOBAL_NOTHING);
only once in the applicatuion startup and the
curl_global_cleanup ();
when the application ends.
Thanks in advance for any help.
Raquel Vieira
Received on 2007-11-12