0

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?

TheEagle
  • 5,808
  • 3
  • 11
  • 39
Gregory B
  • 1
  • 1
  • 2
    ".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 '21 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." https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/setlocale-wsetlocale?view=msvc-160 (this seems like it's a Windows program) But it works fine for me too on my Mac, so... – HTNW Jan 24 '21 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 '21 at 17:45
  • 1
    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 '21 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) https://curl.se/libcurl/c/curl_easy_init.html – Gregory B Jan 24 '21 at 22:12
  • There's nothing particularly wrong with this program. You may have a bad installation of curl and/or ssl/tls libraries. – n. m. could be an AI Jan 24 '21 at 22:15

1 Answers1

0

It turned out that problem was in bad installation. I've reinstalled using this guide (via vcpkg) and now it's all work good!

Gregory B
  • 1
  • 1