cURL / Mailing Lists / curl-library / Single Mail

curl-library

Re: memory leaks after calling curl_multi_perform (fwd)

From: Hong I Ng <gamer19722002_at_yahoo.com>
Date: Wed, 16 Jun 2004 20:49:48 -0700 (PDT)

The version that I use is 7.10.2 (may be I should
upgrade it now) and the OS that I'm using is Mac OS X
10.3.3.

The following is the function that I use to retrieve
web-pages by calling curl's functions. I think I have
freed the array of contents returned by curl.
Otherwise, the strings that are dangling in the memory
may not be just 'text/html'. They would be the
complete HTML pages instead.

unsigned char **
retrieve_search_results(Query *queries, int
number_of_queries)
{
  char error_buffer[CURL_ERROR_SIZE];
  CURL **curls;
  CURLM *curlm;
  CURLMcode curlmcode;
  fd_set fdread, fdwrite, fdexcep;
  int i,
      maxfd,
      time_out_count,
      rc, /* select() return code */
      number_of_curls,
      still_running; /* keep number of running handles
*/
  struct timeval timeout;
  unsigned char **contents;
  
  if(!queries || number_of_queries < 1) return NULL;
  
  /* init a multi stack */
  curlm = curl_multi_init();
  if(!curlm){
    fprintf(stderr, "Something went wrong in
curl_multi_init()\n");
    return NULL;
  }
  
  curls = (CURL **)allocate(number_of_queries,
sizeof(CURL *), NULL);
  contents
    = (unsigned char **)allocate(number_of_queries,
sizeof(unsigned char *),
                                 NULL);
  for(i = number_of_curls = 0; i < number_of_queries;
i++){
    curls[i] = curl_easy_init();
    if(curls[i]){
      if(verbosity() > 1) curl_easy_setopt(curls[i],
CURLOPT_VERBOSE, 1);
      curl_easy_setopt(curls[i], CURLOPT_NOPROGRESS,
1);
      curl_easy_setopt(curls[i], CURLOPT_URL,
queries[i].url);
      curl_easy_setopt(curls[i],
CURLOPT_FOLLOWLOCATION, 1);
      curl_easy_setopt(curls[i], CURLOPT_ERRORBUFFER,
error_buffer);
      curl_easy_setopt(curls[i],
CURLOPT_WRITEFUNCTION, getResourceCallback);
      curl_easy_setopt(curls[i], CURLOPT_WRITEDATA,
(void *)(contents + i));
      curlmcode = curl_multi_add_handle(curlm,
curls[i]);
      if(curlmcode != CURLM_OK && curlmcode !=
CURLM_CALL_MULTI_PERFORM)
        fprintf(stderr, "add handle: %s\n",
curl_multi_error(curlmcode));
      else
        number_of_curls++;
    }
    else
      fprintf(stderr, "Something went wrong in
curl_easy_init(%d)\n", i);
  }
  
  if(number_of_curls){
    /* we start some action by calling perform right
away */
    while(CURLM_CALL_MULTI_PERFORM ==
          curl_multi_perform(curlm, &still_running));
    
    time_out_count = TIME_OUT_SECOND;
    while(still_running && time_out_count > 0){
      FD_ZERO(&fdread);
      FD_ZERO(&fdwrite);
      FD_ZERO(&fdexcep);
      
      /* set a suitable timeout to play around with */
      timeout.tv_sec = 1;
      timeout.tv_usec = 0;
      
      /* get file descriptors from the transfers */
      curlmcode = curl_multi_fdset(curlm, &fdread,
&fdwrite, &fdexcep, &maxfd);
      if(curlmcode != CURLM_OK)
        fprintf(stderr, "while %d still running:
%s\n",
                still_running,
curl_multi_error(curlmcode));

      rc = select(maxfd + 1, &fdread, &fdwrite,
&fdexcep, &timeout);

      switch(rc){
        case -1:
          /* select error */
          break;
        case 0:
          fprintf(stderr, "timeout(%d)... ",
--time_out_count);
        default:
          /* timeout or readable/writable sockets */
          fprintf(stderr, "perform again...");
          while(CURLM_CALL_MULTI_PERFORM ==
                curl_multi_perform(curlm,
&still_running));
          fprintf(stderr, " %d running...\r",
still_running);
          break;
      }
    }
  }
  else{
    for(i = 0; i < number_of_queries; i++)
      contents[i] = (unsigned char
*)freemem(contents[i]);
    contents = (unsigned char **)freemem(contents);
  }

  curlmcode = curl_multi_cleanup(curlm);
  if(curlmcode != CURLM_OK)
    fprintf(stderr, "clean up: %s\n",
curl_multi_error(curlmcode));

  for(i = 0; i < number_of_queries; i++)
curl_easy_cleanup(curls[i]);
  curls = (CURL **)freemem(curls);

  return contents;
}

--- Daniel Stenberg <daniel-curl_at_haxx.se> wrote:
> I'm forwarding your mail to the libcurl mailing list
> instead. This list is
> more suitable for pure libcurl issues.
>
> You didn't mention what libcurl version or what
> operating system you used.
>
> Also, it would help a lot if you could provide a
> sample source code we can run
> in our ends that shows this leak happen!
>
> --
> Daniel Stenberg -- http://curl.haxx.se --
> http://daniel.haxx.se
> Dedicated custom curl help for hire:
> http://haxx.se/curl.html
>
> ---------- Forwarded message ----------
> Date: Wed, 31 Mar 2004 19:06:54 -0800 (PST)
> From: Hong I Ng <gamer19722002_at_yahoo.com>
> To: curl-and-php_at_cool.haxx.se
> Subject: memory leaks after calling
> curl_multi_perform
>
> I've been using cURL (C interface) lately and
> discovered this:
>
> Leak: 0x060ad9b0 size=16 string 'text/html;'
> Call stack: [thread 3d8d4b]: | 0x1000 | start |
> _start | main | test_customize_search_query |
> process_queries | ms | retrieve_next_pages |
> retrieve_search_results | curl_multi_perform |
> Curl_readwrite | malloc | malloc_zone_mallocLeak:
> 0x060ad850 size=16 string 'text/html;'
> Call stack: [thread 94a4b74f]: | 0x1000 | start |
> _start | main | test_customize_search_query |
> process_queries | ms | retrieve_next_pages |
> retrieve_search_results | curl_multi_perform |
> Curl_readwrite | malloc | malloc_zone_malloc
>
> I've made sure that curl_multi_cleanup() has been
> called at the end, and I can't find any other
> functions for cleaning up.
>
> May I know whether the problem lies with me or
> libCURL
> please?
>
> Thanks in advance.
>
> HI
>
> __________________________________
> Do you Yahoo!?
> Yahoo! Small Business $15K Web Design Giveaway
> http://promotions.yahoo.com/design_giveaway/
--- Daniel Stenberg <daniel-curl_at_haxx.se> wrote:
> I'm forwarding your mail to the libcurl mailing list
> instead. This list is
> more suitable for pure libcurl issues.
>
> You didn't mention what libcurl version or what
> operating system you used.
>
> Also, it would help a lot if you could provide a
> sample source code we can run
> in our ends that shows this leak happen!
>
> --
> Daniel Stenberg -- http://curl.haxx.se --
> http://daniel.haxx.se
> Dedicated custom curl help for hire:
> http://haxx.se/curl.html
>
> ---------- Forwarded message ----------
> Date: Wed, 31 Mar 2004 19:06:54 -0800 (PST)
> From: Hong I Ng <gamer19722002_at_yahoo.com>
> To: curl-and-php_at_cool.haxx.se
> Subject: memory leaks after calling
> curl_multi_perform
>
> I've been using cURL (C interface) lately and
> discovered this:
>
> Leak: 0x060ad9b0 size=16 string 'text/html;'
> Call stack: [thread 3d8d4b]: | 0x1000 | start |
> _start | main | test_customize_search_query |
> process_queries | ms | retrieve_next_pages |
> retrieve_search_results | curl_multi_perform |
> Curl_readwrite | malloc | malloc_zone_mallocLeak:
> 0x060ad850 size=16 string 'text/html;'
> Call stack: [thread 94a4b74f]: | 0x1000 | start |
> _start | main | test_customize_search_query |
> process_queries | ms | retrieve_next_pages |
> retrieve_search_results | curl_multi_perform |
> Curl_readwrite | malloc | malloc_zone_malloc
>
> I've made sure that curl_multi_cleanup() has been
> called at the end, and I can't find any other
> functions for cleaning up.
>
> May I know whether the problem lies with me or
> libCURL
> please?
>
> Thanks in advance.
>
> HI
>
> __________________________________
> Do you Yahoo!?
> Yahoo! Small Business $15K Web Design Giveaway
> http://promotions.yahoo.com/design_giveaway/

                
__________________________________
Do you Yahoo!?
Yahoo! Mail - Helps protect you from nasty viruses.
http://promotions.yahoo.com/new_mail
Received on 2004-06-17