相关文章推荐
留胡子的电影票  ·  Install and Set Up ...·  2 周前    · 
英俊的汤圆  ·  使用 sql:max-depth ...·  1 月前    · 
朝气蓬勃的镜子  ·  mysql ...·  6 月前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I've just begun studying libcurl and came across strange behavior of curl_easy_cleanup() function. The task of the program is primitive: to get html code from site and put it in stdout.

#include <cstdio>
#include <clocale>
#include "curl\curl.h"
int main()
    setlocale(LC_ALL, ".utf8");        // deleting this doesn't help
    curl_global_init(CURL_GLOBAL_ALL);
    CURL* curl = curl_easy_init();
    CURLcode res;
    if (curl)
        curl_easy_setopt(curl, CURLOPT_URL, "https://cbr.ru/currency_base/daily/");
        res = curl_easy_perform(curl);
        if (res != CURLE_OK)
            printf("Error\n");
    curl_easy_cleanup(curl);        // crash
    curl_global_cleanup();
    return 0;

All goes well, i have correct output, but curl_easy_cleanup throws access violation exception and program crashes. Why does it do this and how can I fix the problem? Thanks.

UPD: I use Visual Studio 2019 and compile as C++. I came to conclusion, that problem is that site uses https (not http). I tried several http sites and it worked well. How should I treat the https sites then?

".utf8" is a really weird locale. In any case, compiling it, as is, and running only results in an "Error", and no crash. Cannot reproduce. Aside from the weird locale, there's nothing obviously wrong with the shown code. – Sam Varshavchik Jan 24, 2021 at 17:43 @SamVarshavchik Seems perfectly supported: "For example, setlocale(LC_ALL, ".utf8") will use the current default Windows ANSI code page (ACP) for the locale and UTF-8 for the code page." learn.microsoft.com/en-us/cpp/c-runtime-library/reference/… (this seems like it's a Windows program) But it works fine for me too on my Mac, so... – HTNW Jan 24, 2021 at 17:45 It works for me and retrieves the listed web page. What platform are you on? How are you compiling this? I used Visual Studio 2019 and compiled as C++. You are missing curl_global_init and curl_global_cleanup which I've always used in my programs. – Retired Ninja Jan 24, 2021 at 17:45 I do not have problems compiling it as is on Linux. I only had to change curl\curl.h into curl/curl.h – Pat. ANDRIA Jan 24, 2021 at 17:55 @RetiredNinja I also use VS2019 and compile as C++. I've added this two functions but this didn't help. Btw, curl_easy_init calls curl_global_init automatically (though it isn't recommended) curl.se/libcurl/c/curl_easy_init.html – Gregory B Jan 24, 2021 at 22:12

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.