curl / Mailing Lists / curl-library / Single Mail

curl-library

Re: Is it safe to call `curl_global_init` before main()?

From: Victor Abrash <victor_at_speech.sri.com>
Date: Mon, 17 Apr 2017 11:05:13 -0700

On 4/16/17 2:42 AM, Daniel Stenberg wrote:
> On Wed, 12 Apr 2017, curl.greatwolf_at_mamber.net wrote:
>
>> Is it okay to call `curl_global_init` and `curl_global_cleanup`
>> before and after main() respectively?
>
> Yes it probably is. libcurl has no relation to main() so it doesn't
> care if it gets called before or after that entry point. As long as
> the heap is ready to get used etc.
>
> The main thing the curl_global_init function does is setting up third
> party libraries so they are really the ones that need to work in this
> situation.
>
I can confirm that this works.

Best approach in C++ is to put the calls in the constructor/destructor
of a class, with a single static instantiation. This guarantees that
the two Curl functions are only called once per process. You can
initialize other libraries in the same methods. Keep things simple and
don't put any other methods into the class.

This works (at least) on Linux, Windows, and MacOS.

Victor
-------------------------------------------------------------------
Unsubscribe: https://cool.haxx.se/list/listinfo/curl-library
Etiquette: https://curl.haxx.se/mail/etiquette.html
Received on 2017-04-17