macOS: Try to prevent errors when showing a dialog from crashing the game

Also try to use descriptive error names in crash message box on Windows
This commit is contained in:
UnknownShadow200 2023-06-13 23:41:07 +10:00
parent 1a7ed4e60f
commit 9f951893a1
2 changed files with 27 additions and 3 deletions

View File

@ -950,17 +950,32 @@ static void DumpMisc(void* ctx) { }
*--------------------------------------------------Unhandled error logging------------------------------------------------* *--------------------------------------------------Unhandled error logging------------------------------------------------*
*#########################################################################################################################*/ *#########################################################################################################################*/
#if defined CC_BUILD_WIN #if defined CC_BUILD_WIN
static const char* ExceptionDescribe(cc_uint32 code) {
switch (code) {
case EXCEPTION_ACCESS_VIOLATION: return "ACCESS_VIOLATION";
case EXCEPTION_ILLEGAL_INSTRUCTION: return "ILLEGAL_INSTRUCTION";
case EXCEPTION_INT_DIVIDE_BY_ZERO: return "DIVIDE_BY_ZERO";
}
return NULL;
}
static LONG WINAPI UnhandledFilter(struct _EXCEPTION_POINTERS* info) { static LONG WINAPI UnhandledFilter(struct _EXCEPTION_POINTERS* info) {
cc_string msg; char msgBuffer[128 + 1]; cc_string msg; char msgBuffer[128 + 1];
const char* desc;
cc_uint32 code; cc_uint32 code;
cc_uintptr addr; cc_uintptr addr;
DWORD i, numArgs; DWORD i, numArgs;
code = (cc_uint32)info->ExceptionRecord->ExceptionCode; code = (cc_uint32)info->ExceptionRecord->ExceptionCode;
addr = (cc_uintptr)info->ExceptionRecord->ExceptionAddress; addr = (cc_uintptr)info->ExceptionRecord->ExceptionAddress;
desc = ExceptionDescribe(code);
String_InitArray_NT(msg, msgBuffer); String_InitArray_NT(msg, msgBuffer);
String_Format2(&msg, "Unhandled exception 0x%h at %x", &code, &addr); if (desc) {
String_Format2(&msg, "Unhandled %c error at %x", desc, &addr);
} else {
String_Format2(&msg, "Unhandled exception 0x%h at %x", &code, &addr);
}
numArgs = info->ExceptionRecord->NumberParameters; numArgs = info->ExceptionRecord->NumberParameters;
if (numArgs) { if (numArgs) {

View File

@ -156,6 +156,9 @@ void Window_Init(void) {
appHandle = [NSApplication sharedApplication]; appHandle = [NSApplication sharedApplication];
[appHandle activateIgnoringOtherApps:YES]; [appHandle activateIgnoringOtherApps:YES];
Window_CommonInit(); Window_CommonInit();
// NSApplication sometimes replaces the uncaught exception handler, so set it again
NSSetUncaughtExceptionHandler(LogUnhandledNSErrors);
} }
@ -540,11 +543,17 @@ void ShowDialogCore(const char* title, const char* msg) {
CFStringRef titleCF, msgCF; CFStringRef titleCF, msgCF;
NSAlert* alert; NSAlert* alert;
alert = [NSAlert alloc];
alert = [alert init];
titleCF = CFStringCreateWithCString(NULL, title, kCFStringEncodingASCII); titleCF = CFStringCreateWithCString(NULL, title, kCFStringEncodingASCII);
msgCF = CFStringCreateWithCString(NULL, msg, kCFStringEncodingASCII); msgCF = CFStringCreateWithCString(NULL, msg, kCFStringEncodingASCII);
// backwards compatible @try @catch
NS_DURING {
alert = [NSAlert alloc];
alert = [alert init];
} NS_HANDLER {
LogUnhandledNSErrors(localException);
} NS_ENDHANDLER
[alert setMessageText: titleCF]; [alert setMessageText: titleCF];
[alert setInformativeText: msgCF]; [alert setInformativeText: msgCF];
[alert addButtonWithTitle: @"OK"]; [alert addButtonWithTitle: @"OK"];