nxdk icon indicating copy to clipboard operation
nxdk copied to clipboard

Crash when declaring std::locale

Open hcfcoder opened this issue 3 years ago • 3 comments

This line makes the program crash at the beginning, without running a single line. It doesn't matter if the declaration is done as a local variable or as a global one (it fails on both ways):

std::locale const f_portable_locale("C");

The following gist has a minimal program to reproduce this issue:

https://gist.github.com/hcfcoder/326b826dbd13e64f19110bd9905dfaae

hcfcoder avatar Sep 06 '21 23:09 hcfcoder

I have updated the program, removing the SDL functions and library, to make it even more minimal.

hcfcoder avatar Sep 17 '21 12:09 hcfcoder

It appears that the program doesn't actually crash, it exits because it's unable to recover from an error.

We don't really support locales yet, so we're using the stubs included in libc++. Apparently this is not enough to allow constructing a std::locale object though: The constructor contains the line install(new collate_byname<char>(name_)), which calls into https://github.com/XboxDev/nxdk-libcxx/blob/c594e975f383418d98dbdf35a8f2fff4699a62fb/src/locale.cpp#L674

The stubbed newlocale function returns NULL, causing this function to try to throw an exception via __throw_runtime_error, but because C++ exceptions are disabled, that function makes the program quit instead.

I'm not sure why libc++ does it this way, but until we have better locale support, it's probably best to avoid using std::locale at all.

thrimbor avatar Sep 22 '21 00:09 thrimbor

Thank you very much, everything is clear!

As these errors can be hard to debug, would it be worth to put a warning message in the "locale" constructor, so that when someone compiles a program and "locale" is used, a warning message is triggered? I know that it is a bad solution, but leaving as it is can make someone waste a lot of time trying to figure out where is his error...

hcfcoder avatar Sep 22 '21 21:09 hcfcoder