diff --git a/include/instance.hpp b/include/instance.hpp index 635676b..4feab15 100644 --- a/include/instance.hpp +++ b/include/instance.hpp @@ -26,6 +26,9 @@ namespace mastodonpp using std::string; +//! Internal use only. +extern bool curl_initialized; + /*! * @brief Holds the access data of and the connection to an instance. * @@ -39,6 +42,11 @@ public: /*! * @brief Construct a new Instance object. * + * The first construction of an Instance object will call + * `curl_global_init`, which is not thread-safe. For more information + * consult [curl_global_init(3)] + * (https://curl.haxx.se/libcurl/c/curl_global_init.html). + * * @param instance The hostname of the instance. * @param access_token Your access token. * diff --git a/src/instance.cpp b/src/instance.cpp index 6cdc956..17427ad 100644 --- a/src/instance.cpp +++ b/src/instance.cpp @@ -24,16 +24,29 @@ namespace mastodonpp using std::move; +bool curl_initialized{false}; + Instance::Instance(string instance, string access_token) : _instance{move(instance)} , _access_token{move(access_token)} - , _connection{curl_easy_init()} { + if (!curl_initialized) + { + curl_global_init(CURL_GLOBAL_DEFAULT); + curl_initialized = true; + } + _connection = curl_easy_init(); setup_curl(); } Instance::~Instance() { curl_easy_cleanup(_connection); + + if (curl_initialized) + { + curl_global_cleanup(); + curl_initialized = false; + } } int Instance::writer(char *data, size_t size, size_t nmemb, string *writerData)