Ignore:
Timestamp:
Jun 27, 2014, 1:14:39 AM (10 years ago)
Author:
djay
Message:

Remove memory leaks from ZOO-Kernel. Fix issue #99.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/zoo-project/zoo-kernel/ulinet.c

    r478 r490  
    2727
    2828#define _ULINET
     29#define MAX_WAIT_MSECS 180*1000 /* Wait max. 180 seconds */
    2930#include "ulinet.h"
    3031#include <assert.h>
     
    162163#endif
    163164
    164 HINTERNET InternetOpen(char* lpszAgent,int dwAccessType,char* lpszProxyName,char* lpszProxyBypass,int dwFlags){
    165  
     165HINTERNET InternetOpen(char* lpszAgent,int dwAccessType,char* lpszProxyName,char* lpszProxyBypass,int dwFlags){ 
    166166  HINTERNET ret;
    167   struct MemoryStruct header;
     167  ret.handle=curl_multi_init();
     168  ret.ihandle=NULL;
    168169  ret.hasCacheFile=0;
    169170  ret.nDataAlloc = 0;
    170171  ret.mimeType = NULL;
    171 
    172   curl_global_init(CURL_GLOBAL_ALL|CURL_GLOBAL_SSL|CURL_GLOBAL_WIN32);
    173   ret.handle=curl_easy_init();
    174 
    175   curl_easy_setopt(ret.handle, CURLOPT_COOKIEFILE, "ALL");
    176 #ifndef TIGER
    177   curl_easy_setopt(ret.handle, CURLOPT_COOKIELIST, "ALL");
    178 #endif
    179   curl_easy_setopt(ret.handle, CURLOPT_USERAGENT, lpszAgent);
    180  
    181   curl_easy_setopt(ret.handle,CURLOPT_FOLLOWLOCATION,1);
    182   curl_easy_setopt(ret.handle,CURLOPT_MAXREDIRS,3);
    183  
    184   header.memory=NULL;
    185   header.size = 0;
    186 
    187   curl_easy_setopt(ret.handle, CURLOPT_HEADERFUNCTION, header_write_data);
    188   curl_easy_setopt(ret.handle, CURLOPT_WRITEHEADER, (void *)&header);
    189 
    190 #ifdef MSG_LAF_VERBOSE
    191   curl_easy_setopt(ret.handle, CURLOPT_VERBOSE, 1);
    192 #endif
    193 
     172  ret.agent=strdup(lpszAgent);
    194173  return ret;
    195174}
     
    206185    handle.nDataAlloc = handle.nDataLen = 0;
    207186  }
     187  if(handle.ihandle!=NULL)
     188    curl_easy_cleanup(handle.ihandle);
    208189  if(handle.handle)
    209     curl_easy_cleanup(handle.handle);
    210   curl_global_cleanup();
     190    curl_multi_cleanup(handle.handle);
     191  free(handle.agent);
    211192}
    212193
     
    214195
    215196  char filename[255];
     197  struct MemoryStruct header;
     198
    216199  hInternet.nDataLen = 0;
    217200
    218201  hInternet.nDataAlloc = 0;
    219202  hInternet.pabyData= NULL;
     203
     204  if(hInternet.ihandle!=NULL)
     205    curl_easy_cleanup(hInternet.ihandle);
     206  hInternet.ihandle=curl_easy_init( );
     207  curl_easy_setopt(hInternet.ihandle, CURLOPT_COOKIEFILE, "ALL");
     208#ifndef TIGER
     209  curl_easy_setopt(hInternet.ihandle, CURLOPT_COOKIELIST, "ALL");
     210#endif
     211  curl_easy_setopt(hInternet.ihandle, CURLOPT_USERAGENT, hInternet.agent);
     212 
     213  curl_easy_setopt(hInternet.ihandle,CURLOPT_FOLLOWLOCATION,1);
     214  curl_easy_setopt(hInternet.ihandle,CURLOPT_MAXREDIRS,3);
     215 
     216  header.memory=NULL;
     217  header.size = 0;
     218
     219  curl_easy_setopt(hInternet.ihandle, CURLOPT_HEADERFUNCTION, header_write_data);
     220  curl_easy_setopt(hInternet.ihandle, CURLOPT_WRITEHEADER, (void *)&header);
     221
     222#ifdef MSG_LAF_VERBOSE
     223  curl_easy_setopt(hInternet.ihandle, CURLOPT_VERBOSE, 1);
     224#endif
     225
    220226     
    221227  switch(dwFlags)
     
    223229    case INTERNET_FLAG_NO_CACHE_WRITE:   
    224230      hInternet.hasCacheFile=-1;
    225       curl_easy_setopt(hInternet.handle, CURLOPT_WRITEFUNCTION, write_data_into);
    226       curl_easy_setopt(hInternet.handle, CURLOPT_WRITEDATA, &hInternet);
     231      curl_easy_setopt(hInternet.ihandle, CURLOPT_WRITEFUNCTION, write_data_into);
     232      curl_easy_setopt(hInternet.ihandle, CURLOPT_WRITEDATA, &hInternet);
    227233      break;
    228234    default:
     
    236242   
    237243      hInternet.hasCacheFile=1;
    238       curl_easy_setopt(hInternet.handle, CURLOPT_WRITEFUNCTION, NULL);
    239       curl_easy_setopt(hInternet.handle, CURLOPT_WRITEDATA, hInternet.file);
     244      curl_easy_setopt(hInternet.ihandle, CURLOPT_WRITEFUNCTION, NULL);
     245      curl_easy_setopt(hInternet.ihandle, CURLOPT_WRITEDATA, hInternet.file);
    240246      hInternet.nDataLen=0;
    241247      break;
     
    250256#endif
    251257    //curl_easy_setopt(hInternet.handle,CURLOPT_COOKIE,lpszHeaders);
    252     curl_easy_setopt(hInternet.handle,CURLOPT_POST,1);
     258    curl_easy_setopt(hInternet.ihandle,CURLOPT_POST,1);
    253259#ifdef ULINET_DEBUG
    254260    fprintf(stderr,"** (%s) %d **\n",lpszHeaders,dwHeadersLength);
    255     curl_easy_setopt(hInternet.handle,CURLOPT_VERBOSE,1);
    256 #endif
    257     curl_easy_setopt(hInternet.handle,CURLOPT_POSTFIELDS,lpszHeaders);
     261    curl_easy_setopt(hInternet.ihandle,CURLOPT_VERBOSE,1);
     262#endif
     263    curl_easy_setopt(hInternet.ihandle,CURLOPT_POSTFIELDS,lpszHeaders);
    258264    //curl_easy_setopt(hInternet.handle,CURLOPT_POSTFIELDSIZE,dwHeadersLength+1);
    259265    if(hInternet.header!=NULL)
    260       curl_easy_setopt(hInternet.handle,CURLOPT_HTTPHEADER,hInternet.header);
    261   }
    262 
    263   curl_easy_setopt(hInternet.handle,CURLOPT_URL,lpszUrl);
    264   curl_easy_perform(hInternet.handle);
    265   curl_easy_getinfo(hInternet.handle,CURLINFO_CONTENT_TYPE,&hInternet.mimeType);
     266      curl_easy_setopt(hInternet.ihandle,CURLOPT_HTTPHEADER,hInternet.header);
     267  }
     268
     269  curl_easy_setopt(hInternet.ihandle,CURLOPT_URL,lpszUrl);
     270
     271  curl_multi_add_handle(hInternet.handle,hInternet.ihandle);
     272 
     273  int still_running=0;
     274  int msgs_left=0;
     275  do{
     276    curl_multi_perform(hInternet.handle, &still_running);
     277  }while(still_running);
     278
     279  CURLMsg *msg=NULL;
     280  while ((msg = curl_multi_info_read(hInternet.handle, &msgs_left))) {
     281    if (msg->msg == CURLMSG_DONE) {
     282      CURL *eh=NULL;
     283      eh = msg->easy_handle;
     284      curl_easy_getinfo(eh,CURLINFO_CONTENT_TYPE,&hInternet.mimeType);
     285    }
     286    else {
     287      fprintf(stderr, "error: after curl_multi_info_read(), CURLMsg=%d\n", msg->msg);
     288    }
     289  }
     290  curl_multi_remove_handle(hInternet.handle, hInternet.ihandle);
     291
    266292#ifdef ULINET_DEBUG
    267293  fprintf(stderr,"DEBUG MIMETYPE: %s\n",hInternet.mimeType);
     
    274300  memset(&CCookie[0],0,1024);
    275301#ifndef TIGER
    276   curl_easy_setopt(hInternet.handle, CURLOPT_COOKIELIST, "ALL");
     302  curl_easy_setopt(hInternet.ihandle, CURLOPT_COOKIELIST, "ALL");
    277303#endif
    278304  return 1;
Note: See TracChangeset for help on using the changeset viewer.

Search

Context Navigation

ZOO Sponsors

http://www.zoo-project.org/trac/chrome/site/img/geolabs-logo.pnghttp://www.zoo-project.org/trac/chrome/site/img/neogeo-logo.png http://www.zoo-project.org/trac/chrome/site/img/apptech-logo.png http://www.zoo-project.org/trac/chrome/site/img/3liz-logo.png http://www.zoo-project.org/trac/chrome/site/img/gateway-logo.png

Become a sponsor !

Knowledge partners

http://www.zoo-project.org/trac/chrome/site/img/ocu-logo.png http://www.zoo-project.org/trac/chrome/site/img/gucas-logo.png http://www.zoo-project.org/trac/chrome/site/img/polimi-logo.png http://www.zoo-project.org/trac/chrome/site/img/fem-logo.png http://www.zoo-project.org/trac/chrome/site/img/supsi-logo.png http://www.zoo-project.org/trac/chrome/site/img/cumtb-logo.png

Become a knowledge partner

Related links

http://zoo-project.org/img/ogclogo.png http://zoo-project.org/img/osgeologo.png