cURL / Mailing Lists / curl-library / Single Mail

curl-library

memory leak when using zlib lib

From: moneymachine <408885336_at_qq.com>
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 */
  if(ptr)
    return realloc(ptr, size);
  else
    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= "http://www.ip138.com";
  curl_global_init(CURL_GLOBAL_ALL);
 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_URL,detect_url.c_str());
  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_WRITEFUNCTION,WriteMemoryCallback1);
  curl_easy_setopt(curl,CURLOPT_WRITEDATA,(void *)&chunk);
   res = curl_easy_perform(curl);
  curl_easy_cleanup(curl);
 }
  if(chunk.memory)
  free(chunk.memory);
 curl_slist_free_all(slist);
 curl_global_cleanup();
 }
  
  
  
  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: http://cool.haxx.se/list/listinfo/curl-library
Etiquette: http://curl.haxx.se/mail/etiquette.html
Received on 2012-10-02