Hello

I'm using libcurl with C++ classes in a single threaded application by setting 
up the curl env in the constructor and invoking cleanup operation in the 
destructor, but I'm having trouble freeing the allocated memory. I read the 
curl documentation and tutorial.  What is the correct method of freeing 
resources?

Making the following curl function calls In the constructor:
=======================================================
Ctor()
{
curl_global_init(CURL_GLOBAL_ALL);
_cURLHandle = curl_easy_init();
curl_easy_setopt(_cURLHandle, CURLOPT_ERRORBUFFER,_cURLMessages);
}

According to the curl documentation:
 curl_easy_init initializes curl and this call MUST have a corresponding call 
to curl_easy_cleanup(3)<http://curl.haxx.se/libcurl/c/curl_easy_cleanup.html> 
when the operation is complete.

According to the curl documentation:
You should call 
curl_global_cleanup(3)<http://curl.haxx.se/libcurl/c/curl_global_cleanup.html> 
once for each call you make to 
curl_global_init(3)<http://curl.haxx.se/libcurl/c/curl_global_init.html>, after 
you are done using libcurl.

I tried 4 scenarios for cleanup in the destructor

dtor scenario_1
{
        /*Call curl_global_cleanup() immediately before the program exits, when 
the program is again only one thread
         *and after its last use of libcurl. It isn't actually required that 
the functions be called at the beginning
         *and end of the program -- that's just usually the easiest way to do 
it. It is required that the functions
         *be called when no other thread in the program is running.
         */
        curl_global_cleanup();

        /*This function must be the last function to call for an easy session. 
It is the opposite of
         * the curl_easy_init(3) function and must be called with the same 
handle as input that the
         * curl_easy_init call returned.
         */
        curl_easy_cleanup(_cURLFileHandle);
}

Results:Program received signal SIGSEGV, Segmentation fault.
=================================================
#0  0x63912920 in Curl_cookie_loadfiles+0x9c ()
#1  0x63933c74 in flush_cookies+0x8c ()
#2  0x639341ac in Curl_close+0x2ac
#3  0x639624e8 in curl_easy_cleanup+0x3c ()

dtor scenario_2
{
        /*This function must be the last function to call for an easy session. 
It is the opposite of
         * the curl_easy_init(3) function and must be called with the same 
handle as input that the
         * curl_easy_init call returned.
         */
        curl_easy_cleanup(_cURLFileHandle);

        /*Call curl_global_cleanup() immediately before the program exits, when 
the program is again only one thread
         *and after its last use of libcurl. It isn't actually required that 
the functions be called at the beginning
         *and end of the program -- that's just usually the easiest way to do 
it. It is required that the functions
         *be called when no other thread in the program is running.
         */
        curl_global_cleanup();
}
Results:Program received signal SIGSEGV, Segmentation fault.
=================================================
#0  0x6388d920 in Curl_cookie_loadfiles+0x9c ()
#1  0x638aec74 in flush_cookies+0x8c
#2  0x638af1ac in Curl_close+0x2ac
#3  0x638dd4e8 in curl_easy_cleanup+0x3c ()

dtor scenario_3
{
        /*Call curl_global_cleanup() immediately before the program exits, when 
the program is again only one thread
         *and after its last use of libcurl. It isn't actually required that 
the functions be called at the beginning
         *and end of the program -- that's just usually the easiest way to do 
it. It is required that the functions
         *be called when no other thread in the program is running.
         */
        curl_global_cleanup();
}
Results:Memory Leak.
=================================================
      Memory leaked: 37013 bytes (60.9%); potentially leaked: 0 bytes (0%)
         MLK: 34256 bytes leaked at 0x401c49d0
         This memory was allocated from:
               malloc         [rtlib.o]
               calloc         [rtlib.o]
               Curl_open      [libcurl.sl.5.1]
               curl_easy_init [libcurl.sl.5.1]
               csCtkXmlEntityResolver::setUpCurlEnvironment() 
[csCtkXmlEntityResolver.C:286]
                           * for transferring. You must never share the same 
handle in multiple threads. curl_easy_init initializes
                           * curl and this call MUST have a corresponding call 
to curl_easy_cleanup() when the operation is complete.
                           */
               =>         _cURLHandle = curl_easy_init();

                          //If this function returns NULL, something went wrong 
and you cannot use the other curl functions.
                          if(_cURLHandle)
               csCtkXmlEntityResolver::csCtkXmlEntityResolver(const char 
*,bool)%2 [csCtkXmlEntityResolver.C:56]
               csCtkXmlDOMParser::parseDocumentUseExternalDTD(char *,char *) 
[csCtkXmlDOMParser.C:907]
               csCtkXmlDOMParser::isXmlValidUseExternalDTD(char *,const char *) 
[csCtkXmlDOMParser.C:661]
               csCtkXmlDOMParser::doValidateAgainstExternalDtd(char *,char *) 
[csCtkXmlDOMParser.C:291]
               csCtkXmlDOMParser::doValidateAgainstExternalDtd(char *) 
[csCtkXmlDOMParser.C:298]
               doXmlParserSelfTest(int,char **) [testMe.C:109]
               main           [testMe.C:59]

dtor scenario_4
{
        /*This function must be the last function to call for an easy session. 
It is the opposite of
         * the curl_easy_init(3) function and must be called with the same 
handle as input that the
         * curl_easy_init call returned.
         */
        curl_easy_cleanup(_cURLFileHandle);
}
Results:Program received signal SIGSEGV, Segmentation fault.
=================================================
#0  0x6388d920 in Curl_cookie_loadfiles+0x9c
#1  0x638aec74 in flush_cookies+0x8c ()
#2  0x638af1ac in Curl_close+0x2ac
#3  0x638dd4e8 in curl_easy_cleanup+0x3c ()


-------------------------------------------------------------------
List admin: http://cool.haxx.se/list/listinfo/curl-library
Etiquette:  http://curl.haxx.se/mail/etiquette.html

Reply via email to