When I was testing the program, I switched to French and clicked a button, and suddenly the program crashed. I checked the code and found that the problem was here.
memset(tempChar,0,sizeof(tempChar));
//g_print("%s\n", StringSystem::get_string("Draw-A-Card-Remaining-Number").c_str());
sprintf(tempChar,"%s: %d", StringSystem::get_string("Draw-A-Card-Remaining-Number").c_str(),g_totalCards-g_totalPickedCards);The crash occurs at the sprintf function. I use g_print to print strings beforehand, and it can print normal strings, and the program can run normally. Once the g_print function is commented out, the program crashes. It looks very strange.
I discovered that the problem was actually insufficient space in tempChar. At first, I just treated it as storage space for numeric strings and only allocated 8 bytes of space. Later, I didn't pay attention and directly used it to store strings. For English, it didn't fill up the 8 bytes, but French is longer, which led to insufficient memory and overflow.
The solution is very simple, just allocate a bit more space.
Why does the program run normally after using g_print to print a string? According to the AI's explanation:
The extra function call changed the stack layout, causing the buffer overflow to temporarily not overwrite critical data. Buffer overflow is undefined behavior, and sometimes it 'appears normal' is just a coincidence.