cURL / Mailing Lists / curl-library / Single Mail


memory leak when using zlib lib

From: moneymachine <>
Date: Tue, 2 Oct 2012 17:37:16 +0800

im using winxp+vs2008+curl7.27+zlib1.2.7
 zlib and curl all complied by /MT into libcurl.lib
 code below:

 void *myrealloc1(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 */
    return realloc(ptr, size);
    return malloc(size);
 size_t WriteMemoryCallback1(void *ptr, size_t size, size_t nmemb, void *data)
 size_t realsize = size * nmemb;
 struct MemoryStruct *mem = (struct MemoryStruct *)data;
  mem->memory = (char *)myrealloc1(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;
 void GetNewHtmlSource()
 CURL * curl;
 CURLcode res;
  struct MemoryStruct chunk;
  chunk.memory=NULL; /* we expect realloc(NULL, size) to work */
 chunk.size = 0; /* no data at this point */
  string detect_url;
  detect_url= "";
 curl = curl_easy_init();
  struct curl_slist *slist = NULL;
 slist = curl_slist_append(slist, NULL);
 slist = curl_slist_append(slist, "Accept-Language: en-us");
 slist = curl_slist_append(slist, "Connection: Keep-Alive");
  if (curl)
  curl_easy_setopt(curl, CURLOPT_HEADER, NULL);
   curl_easy_setopt(curl, CURLOPT_USERAGENT, "Mozilla/5.0");
  curl_easy_setopt(curl, CURLOPT_ENCODING, "gzip, deflate");
  curl_easy_setopt(curl, CURLOPT_HTTPHEADER, slist);
   curl_easy_setopt(curl, CURLOPT_TIMEOUT, 60);
  curl_easy_setopt(curl,CURLOPT_WRITEDATA,(void *)&chunk);
   res = curl_easy_perform(curl);
  after runing GetNewHtmlSource() in the multithread 100000 time done, about 20m memory increased.
 I dont know where the memory leak, pls help.
 I try the zlib 1.2.6 1.2.5, all have the leak problems.
 BTW, if not complied including zlib and not using CURLOPT_ENCODING, there is no memory leak problem.

 thank you.

List admin:
Received on 2012-10-02