Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

<locale>: Cannot run in Release mode and throw unhandled exception: std::runtime_error - UTF8 to GBK #1068

Open
fsb4000 opened this issue Jul 21, 2020 · 4 comments
Labels
bug Something isn't working

Comments

@fsb4000
Copy link
Contributor

fsb4000 commented Jul 21, 2020

Command-line test case

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string>
#include <locale>
#include <cstdlib>
using namespace std;

int main()
{
    auto func = [](const string& src) -> string
    {
        const auto srcCode = ".65001", dstCode = ".936";
        const size_t& _max_char_count = 3;
        std::wstring wsz;
        std::string csz;
        const size_t& wmax_length = src.length() + 1;
        wchar_t* wtmp = new wchar_t[wmax_length];
        const std::locale& prev = std::locale::global(std::locale(srcCode));
        const std::size_t& wret = std::mbstowcs(wtmp, src.c_str(), wmax_length);
        wsz = std::wstring(wtmp, wret);
        delete[]wtmp;

        const size_t& cmax_length = wsz.length() * _max_char_count + 1;
        char* ctmp = new char[cmax_length];
        std::locale::global(std::locale(dstCode));
        const std::size_t& cret = std::wcstombs(ctmp, wsz.c_str(), cmax_length);
        csz = std::string(ctmp, cret);
        delete[]ctmp;
        std::locale::global(prev);
        return csz;
    };
    std::system("chcp 936 >NUL");
    cout << func(u8"你好") << endl;

    system("pause");
    return 0;
}

Expected behavior
Should print to console the two characters above.

Actually it works on Windows 10 Pro 20.04 (tested X64 Release, X86 Release, X64 Debug, X86 Debug with chcp 936 and without chcp 936)
win10
win10

And I have the error on Windows 7 SP1 Home Premium(also with and without chcp 936)
(error message: "invalid code page specified", same message as standalone command "chcp 936" )
win7
win7

STL version
Microsoft Visual Studio Community 2019 Version 16.6.4

Additional context
See also: VSO-811511 / AB#811511 | DevCom-471144

@fsb4000
Copy link
Contributor Author

fsb4000 commented Jul 21, 2020

I add catch(const std::exception& e) and at Windows 7 I have exception: "bad locale name".

win7

Maybe it's ok.

@fsb4000 fsb4000 mentioned this issue Jul 21, 2020
58 tasks
@AlexGuteniev

This comment has been minimized.

@fsb4000

This comment has been minimized.

@StephanTLavavej StephanTLavavej added the bug Something isn't working label Jul 30, 2020
@StephanTLavavej
Copy link
Member

This may be by design, related to OS support for UTF-8. Thanks for porting this bug from DevCom!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants