cURL / Mailing Lists / curl-library / Single Mail


Problem with my class in independents threads- Error: "Failure when receiving data from the peer"

From: Denis Bondarev <>
Date: Mon, 27 Jul 2009 01:17:27 +0300

Hello. My name is Denis. I am programming on Builder6 C++ and use dinamic
libcurl (libcurl.dll). I have a problem, that hurts me very many time.
I write a class on my Builder 6 C++ that here full text:

#include <curl/curl.h>
#include <curl/easy.h>
#include <curl/types.h>

class GCurl


CURLcode res;

  CURL*(__stdcall *curl_easy_init)();
CURLcode(__stdcall *curl_easy_setopt )(CURL *curl, CURLoption option, ...);
CURLcode (__stdcall *curl_easy_perform )(CURL *curl);
CURLcode(__stdcall *curl_easy_getinfo )(CURL *curl, CURLINFO info, ...);
void (__stdcall *curl_easy_cleanup )(CURL *curl);
struct curl_slist *(__stdcall *curl_slist_append )(struct curl_slist
*list,const char *string);
void (__stdcall *curl_slist_free_all)(struct curl_slist * list);
 void (__stdcall *curl_global_cleanup)();

 AnsiString GCurl_Error;

 static size_t Writer(char *data, size_t size, size_t nmemb, AnsiString
      size_t result = 0;

      if(buffer != NULL)
          result = size * nmemb;

       //delete data;

      delete &nmemb;

      return result;
     delete &result;
     delete buffer;
      } ;

int GCurl::Init(int r)
 cl = NULL;
 if( ( cl = LoadLibrary( "libcurl.dll" ) ) == NULL )
    MessageBox(NULL, "I can't load libcurl","ERROR", 0);
 curl_easy_init =(CURL*(__stdcall*)())GetProcAddress(cl, "curl_easy_init" );
curl_easy_setopt =(CURLcode(__stdcall *)(CURL *curl, CURLoption option,
...))GetProcAddress(cl, "curl_easy_setopt");
curl_easy_perform =(CURLcode(__stdcall *)(CURL *curl))GetProcAddress(cl,
"curl_easy_perform" );
curl_easy_cleanup =(void (__stdcall *)(CURL *curl))GetProcAddress(cl,
"curl_easy_cleanup" );
curl_easy_getinfo=(CURLcode(__stdcall *)(CURL *curl, CURLINFO info,
...))GetProcAddress(cl, "curl_easy_getinfo");
curl_slist_append=(curl_slist*(__stdcall *)(struct curl_slist *list,const
char *string))GetProcAddress(cl,"curl_slist_append");
curl_slist_free_all=(void(__stdcall *)(struct curl_slist
curl_global_cleanup=(void(__stdcall *)())GetProcAddress(cl,
"curl_global_cleanup" );


   AnsiString GCurl::FuncHTTPGet(int Time_Wait,AnsiString url)
 char errbuf[CURL_ERROR_SIZE];
 CURL *curl;
 String table;
 curl = curl_easy_init();

curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION,&Writer ); // !

  curl_easy_setopt(curl, CURLOPT_WRITEDATA , &table);

  curl_easy_setopt(curl, CURLOPT_URL, url);

   curl_easy_setopt(curl, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U;
Windows NT 5.1; en-US; rv: Gecko/20061204 Firefox/");
 curl_easy_setopt(curl, CURLOPT_TIMEOUT,Time_Wait);
 curl_easy_setopt(curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION,1);
curl_easy_setopt(curl, CURLOPT_COOKIEJAR, "outfile.txt");
curl_easy_setopt ( curl, CURLOPT_COOKIEFILE, "outfile.txt" );

curl_easy_setopt(curl, CURLOPT_HEADER , 1 );

curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, errbuf);

return table;
delete curl;
delete errbuf;
delete &table;
delete &url;
delete &Time_Wait;


This class must just return HTTP-text of my URL. I cause his work in next
code from my general program:

GCurl*g= new GCurl();
RichEdit1->Text= (g->FuncHTTPGet(600,""));

After this: RichEdit1 have HTTP-text from URL and Memo1 have error-text.
It is real good work! But if I create two Threads for independents works and
in each thread I created new RichEdit,Memo and GCurl objects and run
this threads together - I have error: "Failure when receiving data from the
peer"!!!!! But why????? I created
 independents objects of my class with independents RichEdit and Memo
objects in independents threads! Why this error is true?????
Please help me solve this problem.

Best regards from me.
Received on 2009-07-27