ereOn
ereOn

Reputation: 55776

What is cURL option parameters lifetime?

I'm using cURL for the first time and while the documentation seems rather complete, I fail to see where I can find the required lifetime for the curl_easy_setopt() parameters.

Here is an example of what I mean:

char* str = strdup("my user agent");
curl_easy_setopt(curl_handle, CURLOPT_USERAGENT, str);
free(str);

Is curl_handle valid after the call to free() ?

Has str content been copied or is it still referenced directly by curl somewhere ?

Upvotes: 2

Views: 988

Answers (1)

user529758
user529758

Reputation:

From the official docs:

Strings passed to libcurl as 'char *' arguments, are copied by the library; thus the string storage associated to the pointer argument may be overwritten after curl_easy_setopt() returns. Exceptions to this rule are described in the option details below.

Before version 7.17.0, strings were not copied. Instead the user was forced keep them available until libcurl no longer needed them.

So if you use a recent version of libcurl, you can free the string immediately after setting it.

Upvotes: 5

Related Questions