Home > database >  libcurl - get content_type data (not free pointer)
libcurl - get content_type data (not free pointer)

Time:12-15

Please tell me one moment related to the option CURLINFO_CONTENT_TYPE:

char *cont_type_pointer = NULL;
curl_easy_getinfo(curl, CURLINFO_CONTENT_TYPE, &cont_type_pointer );

The "cont_type_pointer" pointer will be NULL or pointing to private memory you MUST NOT free it - it gets freed when you call curl_easy_cleanup on the corresponding CURL handle.

This thing is unclear: "you MUST NOT free it - it gets freed when you call curl_easy_cleanup"

And if after this request I do not call curl_easy_cleanup() and immediately make another request in which I also need to get data CURLINFO_CONTENT_TYPE ?? Will it be a memory leak ?

The memory from the last request was not released by the "cont_type_pointer" pointer, and I am already making the second request. It's not clear.

CodePudding user response:

Since libcurl is an OpenSource project, we could easilly check that ourself. curl_easy_getinfo with the flag CURLINFO_CONTENT_TYPE just returns internal field from the CURL* struct:

case CURLINFO_CONTENT_TYPE:
    *param_charp = data->info.contenttype;

Hence the requirement not to free its memory after use.

According to the Curl documentation you are even encouraged for multiple curl_easy_perform calls to share existing connection and calling curl_easy_cleanup only after everything is done:

You can do any amount of calls to curl_easy_perform while using the same easy_handle. If you intend to transfer more than one file, you are even encouraged to do so. libcurl will then attempt to re-use the same connection for the following transfers, thus making the operations faster, less CPU intense and using less network resources. Just note that you will have to use curl_easy_setopt between the invokes to set options for the following curl_easy_perform.

If you look deep inside curl_easy_perform you can see, that libcurl uses internal linked list to store connection data from all calls. When you finally call curl_easy_cleanup all memory will be cleaned and there will be no leak.

  • Related