Skip to content

Commit 2f59860

Browse files
committed
Fix MinGW 14 locale crash: initialize locale early on Windows
1 parent 77f7ed4 commit 2f59860

2 files changed

Lines changed: 26 additions & 2 deletions

File tree

mode/CppMode.jar

0 Bytes
Binary file not shown.

src/Processing.cpp

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,17 @@ static void _gluLookAt(double ex,double ey,double ez,
8787
}
8888

8989
// stb_truetype -- drop stb_truetype.h next to this file for TTF font rendering.
90+
// Workaround for MinGW 14+ locale initialization crash on Windows
91+
#ifdef _WIN32
92+
struct _LocaleInit {
93+
_LocaleInit() {
94+
// Initialize locale early to prevent crash in ios_base::_M_init
95+
try { ::std::locale::global(::std::locale("C")); } catch(...) {}
96+
}
97+
};
98+
static _LocaleInit _locale_init_instance;
99+
#endif
100+
90101
// default.ttf in the project root is loaded automatically as the default font.
91102

92103

@@ -148,7 +159,8 @@ static void _doEnableDebugConsole() {
148159
}
149160
void enableDebugConsole() { _doEnableDebugConsole(); }
150161

151-
static ::std::string _s_objDir; // OBJ loader scratch
162+
static ::std::string* _s_objDir_ptr = nullptr;
163+
static ::std::string& _s_objDir = *(_s_objDir_ptr ? _s_objDir_ptr : (_s_objDir_ptr = new ::std::string()));
152164

153165
static const int PERLIN_YWRAPB = 4;
154166
static const int PERLIN_YWRAP = 1 << PERLIN_YWRAPB; // 16
@@ -3402,6 +3414,7 @@ void PApplet::run(){
34023414
});
34033415
glfwMakeContextCurrent(gWindow);
34043416
glewExperimental = GL_TRUE;
3417+
glewExperimental = GL_TRUE;
34053418
GLenum glewErr = glewInit();
34063419
if(glewErr != GLEW_OK){
34073420
#ifdef _WIN32
@@ -3440,6 +3453,9 @@ void PApplet::run(){
34403453
glfwSetWindowSizeCallback(gWindow, winsize_cb);
34413454
glfwSetWindowFocusCallback(gWindow, focus_cb);
34423455
glfwSetWindowPosCallback(gWindow, winpos_cb);
3456+
#ifdef _WIN32
3457+
__try {
3458+
#endif
34433459
focused=(glfwGetWindowAttrib(gWindow,GLFW_FOCUSED)==GLFW_TRUE);
34443460

34453461
// Auto-load default.ttf from project root as the default font
@@ -3493,6 +3509,13 @@ void PApplet::run(){
34933509
}
34943510

34953511
glfwFocusWindow(gWindow);
3512+
#ifdef _WIN32
3513+
} __except(EXCEPTION_EXECUTE_HANDLER) {
3514+
char msg[512];
3515+
snprintf(msg,sizeof(msg),"SEH Exception 0x%08lX\n\nThis crash occurred during window initialization.\nPlease report this to the CppMode developers.", GetExceptionCode());
3516+
MessageBoxA(NULL,msg,"CppMode Crash",MB_OK|MB_ICONERROR);
3517+
}
3518+
#endif
34963519

34973520
// Settle loop: poll+swap several times BEFORE this->setup() runs so i3/tiling WMs
34983521
// fully resize the window first. winsize_cb fires during these polls and
@@ -4768,7 +4791,8 @@ PFont PApplet::loadFont(const ::std::string& filename) {
47684791
}
47694792

47704793
// createFont -- creates font from a name/path and size
4771-
static ::std::vector<PFont> _fontPool;
4794+
static ::std::vector<PFont>* _fontPool_ptr = nullptr;
4795+
static ::std::vector<PFont>& _fontPool = *(_fontPool_ptr ? _fontPool_ptr : (_fontPool_ptr = new ::std::vector<PFont>()));
47724796
PFont* PApplet::createFont(const ::std::string& name, float size, bool /*smooth*/) {
47734797
PFont f(name, size);
47744798
// Try as file path first, then common system paths

0 commit comments

Comments
 (0)