libcurl - 获取 content_type 数据(不是自由指针)

libcurl - get content_type data (not free pointer)

请告诉我有关选项的片刻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.

这件事不清楚:“你不能释放它 - 当你调用 curl_easy_cleanup 时它会被释放”

如果在这个请求之后我没有调用 curl_easy_cleanup() 并立即发出另一个请求,我也需要在其中获取数据 CURLINFO_CONTENT_TYPE ??会不会是内存泄漏?

上次请求的内存没有被“cont_type_pointer”指针释放,我已经在进行第二次请求了。 不清楚。

由于 libcurl 是一个开源项目,我们可以很容易地自己检查。 curl_easy_getinfo 带有标志 CURLINFO_CONTENT_TYPE just returns 来自 CURL* 结构的内部字段:

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

因此要求在使用后不要释放其内存。

根据 Curl documentation,您甚至被鼓励进行多次 curl_easy_perform 调用以共享现有连接并仅在一切完成后才调用 curl_easy_cleanup

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.

如果深入研究 curl_easy_perform,您会发现 libcurl 使用内部链表来存储来自所有调用的连接数据。当您最终调用 curl_easy_cleanup 时,所有内存都将被清理,不会有泄漏。