Skip to content

Commit

Permalink
Run curl_global_cleanup only once per application
Browse files Browse the repository at this point in the history
  • Loading branch information
HowardHinnant committed Mar 12, 2021
1 parent 0e08b94 commit b899774
Showing 1 changed file with 15 additions and 11 deletions.
26 changes: 15 additions & 11 deletions src/tz.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2846,24 +2846,29 @@ file_exists(const std::string& filename)

// CURL tools

static
int
curl_global()
namespace
{
if (::curl_global_init(CURL_GLOBAL_DEFAULT) != 0)
throw std::runtime_error("CURL global initialization failed");
return 0;
}

namespace
struct curl_global_init_and_cleanup
{
~curl_global_init_and_cleanup()
{
::curl_global_cleanup();
}
curl_global_init_and_cleanup()
{
if (::curl_global_init(CURL_GLOBAL_DEFAULT) != 0)
throw std::runtime_error("CURL global initialization failed");
}
curl_global_init_and_cleanup(curl_global_init_and_cleanup const&) = delete;
curl_global_init_and_cleanup& operator=(curl_global_init_and_cleanup const&) = delete;
};

struct curl_deleter
{
void operator()(CURL* p) const
{
::curl_easy_cleanup(p);
::curl_global_cleanup();
}
};

Expand All @@ -2873,8 +2878,7 @@ static
std::unique_ptr<CURL, curl_deleter>
curl_init()
{
static const auto curl_is_now_initiailized = curl_global();
(void)curl_is_now_initiailized;
static const curl_global_init_and_cleanup _{};
return std::unique_ptr<CURL, curl_deleter>{::curl_easy_init()};
}

Expand Down

0 comments on commit b899774

Please sign in to comment.