<locale>: Cannot run in Release mode and throw unhandled exception: std::runtime_error - UTF8 to GBK
Nobody has claimed this yet.
- Dominant language
- C++
- Stars
- 11.1k
- Forks
- 1.7k
- Avg merge
- 4d 15h
- Merged PRs (30d)
- 22
Description
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)


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" )


STL version
Microsoft Visual Studio Community 2019 Version 16.6.4
Additional context
See also: VSO-811511 / AB#811511 | DevCom-471144
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by building and running the provided command-line test on Windows 7 SP1, with and without chcp 936, and compare it with Windows 10. Trace the locale conversion around std::locale, mbstowcs, and wcstombs; done means the sample no longer reports an invalid code page or throws and prints the two expected characters.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- cpp
- Domain
- operating-systems
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100