cURL / Mailing Lists / curl-library / Single Mail

curl-library

Problem using c++ functors for CURLOPT_HEADERFUNCTION

From: pepone.onrez <pepone.onrez_at_gmail.com>
Date: Tue, 29 May 2007 01:44:14 +0200

When i set CURLOPT_HEADERFUNCTION to my fuctor i get Segmentation
Fault form libcurl in

#1 0xb6a7fcff in Curl_client_write () from /usr/lib/libcurl.so.3

writeHeaderFunctor = new
TSpecificFunctor<WebBrowser>(this,&WebBrowser::writeHeader);
curl_easy_setopt(_handle,CURLOPT_HEADERFUNCTION ,writeHeaderFunctor);
curl_easy_perform(_handle);

I tes my fuctor whith the following code and its work
    void* ptr,*stream;
    size_t size,nmemb;
    (*writeHeaderFunctor)(ptr,size,nmemb,stream);

Any ideas because libcurl crash when try call the writeHeaderFunctor

Heare is the complete source
----- SOURCE ----

size_t Oz::WebBrowser::writeHeader( void *ptr, size_t size, size_t
nmemb, void *stream){
    std::cerr<<"WriteFuctor write"<<std::endl;
    /*std::string data = reinterpret_cast<char*>(ptr);
    std::cerr<<data<<std::endl;*/
    return size*nmemb;
}

Oz::WebBrowser::WebBrowser(){
    curl_global_init(CURL_GLOBAL_ALL);
    _handle = curl_easy_init( );
    writeHeaderFunctor = new
TSpecificFunctor<WebBrowser>(this,&WebBrowser::writeHeader);
    _responseCode = -1;
}

Oz::WebBrowser::~WebBrowser(){
    curl_easy_cleanup(_handle);
}

void Oz::WebBrowser::get(const std::string& url){
    std::string address = url;
    curl_easy_setopt(_handle,CURLOPT_URL,url.c_str());
    curl_easy_setopt(_handle,CURLOPT_HEADER,1);
    writeHeaderFunctor = new
TSpecificFunctor<WebBrowser>(this,&WebBrowser::writeHeader);
    curl_easy_setopt(_handle,CURLOPT_HEADERFUNCTION ,writeHeaderFunctor);
    curl_easy_perform(_handle);
}
----HEADER FILE --

namespace Oz{
    class WebBrowser;

    class WriteFunctor{
        public:
        virtual size_t operator()(void *ptr, size_t size, size_t
nmemb, void *stream)=0; // call using operator
    };

   // derived template class
   template <class TClass> class TSpecificFunctor : public WriteFunctor{
        private:
        size_t (TClass::*fpt)(void *ptr, size_t size, size_t nmemb,
void *stream); // pointer to member function
        TClass* pt2Object; // pointer to object
        public:

        // constructor - takes pointer to an object and pointer to a
member and stores
        // them in two private variables
        TSpecificFunctor(TClass* _pt2Object, size_t
(TClass::*_fpt)(void *ptr, size_t size, size_t nmemb, void *stream))
            { pt2Object = _pt2Object; fpt=_fpt; };

      // override operator "()"
        virtual size_t operator()(void *ptr, size_t size, size_t
nmemb, void *stream)
            { return (*pt2Object.*fpt)(ptr,size,nmemb,stream);};
       // execute member function
   };

    class WebBrowser{
        private:
        WriteFunctor* writeHeaderFunctor;
        CURL* _handle;
        long _responseCode;
        public:
        WebBrowser();
        ~WebBrowser();
        void get(const std::string& url);
        size_t writeHeader(void *ptr,size_t size,size_t nmemb,void *stream);
    };
}
Received on 2007-05-29