diff --git a/src/Config.h b/src/Config.h index 9b554263..7dc8efb9 100644 --- a/src/Config.h +++ b/src/Config.h @@ -30,7 +30,7 @@ #define VER_LAMEXP_MINOR_LO 7 #define VER_LAMEXP_TYPE Alpha #define VER_LAMEXP_PATCH 6 -#define VER_LAMEXP_BUILD 1212 +#define VER_LAMEXP_BUILD 1214 /////////////////////////////////////////////////////////////////////////////// // Tool versions (minimum expected versions!) diff --git a/src/Global.cpp b/src/Global.cpp index 44f49ebd..d3a897f0 100644 --- a/src/Global.cpp +++ b/src/Global.cpp @@ -479,14 +479,7 @@ bool lamexp_detect_wine(void) */ LONG WINAPI lamexp_exception_handler(__in struct _EXCEPTION_POINTERS *ExceptionInfo) { - if(GetCurrentThreadId() != g_main_thread_id) - { - HANDLE mainThread = OpenThread(THREAD_TERMINATE, FALSE, g_main_thread_id); - if(mainThread) TerminateThread(mainThread, ULONG_MAX); - } - - FatalAppExit(0, L"Unhandeled exception handler invoked, application will exit!"); - TerminateProcess(GetCurrentProcess(), -1); + lamexp_fatal_exit(L"Unhandeled exception handler invoked, application will exit!"); return LONG_MAX; } @@ -495,15 +488,7 @@ LONG WINAPI lamexp_exception_handler(__in struct _EXCEPTION_POINTERS *ExceptionI */ void lamexp_invalid_param_handler(const wchar_t* exp, const wchar_t* fun, const wchar_t* fil, unsigned int, uintptr_t) { - if(GetCurrentThreadId() != g_main_thread_id) - { - HANDLE mainThread = OpenThread(THREAD_TERMINATE, FALSE, g_main_thread_id); - if(mainThread) TerminateThread(mainThread, ULONG_MAX); - - } - - FatalAppExit(0, L"Invalid parameter handler invoked, application will exit!"); - TerminateProcess(GetCurrentProcess(), -1); + lamexp_fatal_exit(L"Invalid parameter handler invoked, application will exit!"); } /* @@ -523,13 +508,9 @@ static void lamexp_console_color(FILE* file, WORD attributes) */ void lamexp_message_handler(QtMsgType type, const char *msg) { - static volatile bool bFatalFlag = false; static const char *GURU_MEDITATION = "\n\nGURU MEDITATION !!!\n\n"; - if(bFatalFlag || (msg == NULL)) - { - return; //We are about to terminate, discard any further messages! - } + if(msg == NULL) return; QMutexLocker lock(&g_lamexp_message_mutex); @@ -552,7 +533,6 @@ void lamexp_message_handler(QtMsgType type, const char *msg) { case QtCriticalMsg: case QtFatalMsg: - bFatalFlag = true; fflush(stdout); fflush(stderr); lamexp_console_color(stderr, FOREGROUND_RED | FOREGROUND_INTENSITY); @@ -597,19 +577,10 @@ void lamexp_message_handler(QtMsgType type, const char *msg) OutputDebugStringA(temp.toLatin1().constData()); } - if(bFatalFlag) + if((type == QtCriticalMsg) || (type == QtFatalMsg)) { lock.unlock(); - - if(GetCurrentThreadId() != g_main_thread_id) - { - HANDLE mainThread = OpenThread(THREAD_TERMINATE, FALSE, g_main_thread_id); - if(mainThread) TerminateThread(mainThread, ULONG_MAX); - } - - MessageBoxW(NULL, QWCHAR(QString::fromUtf8(msg)), L"LameXP - GURU MEDITATION", MB_ICONERROR | MB_TOPMOST | MB_TASKMODAL); - FatalAppExit(0, L"The application has encountered a critical error and will exit now!"); - TerminateProcess(GetCurrentProcess(), -1); + lamexp_fatal_exit(L"The application has encountered a critical error and will exit now!", QWCHAR(QString::fromUtf8(msg))); } } @@ -849,15 +820,7 @@ static unsigned int __stdcall lamexp_debug_thread_proc(LPVOID lpParameter) { Sleep(250); } - if(HANDLE thrd = OpenThread(THREAD_TERMINATE, FALSE, g_main_thread_id)) - { - if(TerminateThread(thrd, -1)) - { - FatalAppExit(0, L"Not a debug build. Please unload debugger and try again!"); - } - CloseHandle(thrd); - } - TerminateProcess(GetCurrentProcess(), -1); + lamexp_fatal_exit(L"Not a debug build. Please unload debugger and try again!"); return 666; } @@ -868,8 +831,7 @@ static HANDLE lamexp_debug_thread_init(void) { if(lamexp_check_for_debugger()) { - FatalAppExit(0, L"Not a debug build. Please unload debugger and try again!"); - TerminateProcess(GetCurrentProcess(), -1); + lamexp_fatal_exit(L"Not a debug build. Please unload debugger and try again!"); } return (HANDLE) _beginthreadex(NULL, 0, lamexp_debug_thread_proc, NULL, 0, NULL); @@ -1019,8 +981,7 @@ static bool lamexp_event_filter(void *message, long *result) { if((!(LAMEXP_DEBUG)) && lamexp_check_for_debugger()) { - FatalAppExit(0, L"Not a debug build. Please unload debugger and try again!"); - TerminateProcess(GetCurrentProcess(), -1); + lamexp_fatal_exit(L"Not a debug build. Please unload debugger and try again!"); } switch(reinterpret_cast(message)->message) @@ -2301,8 +2262,7 @@ static DWORD lamexp_entry_check(void) volatile DWORD retVal = 0xA199B5AF; if(g_lamexp_entry_check_flag != 0x8761F64D) { - FatalAppExit(0, L"Application initialization has failed, take care!"); - TerminateProcess(GetCurrentProcess(), -1); + lamexp_fatal_exit(L"Application initialization has failed, take care!"); } return retVal; } @@ -2318,14 +2278,11 @@ extern "C" { if((!LAMEXP_DEBUG) && lamexp_check_for_debugger()) { - FatalAppExit(0, L"Not a debug build. Please unload debugger and try again!"); - TerminateProcess(GetCurrentProcess(), -1); + lamexp_fatal_exit(L"Not a debug build. Please unload debugger and try again!"); } - if(g_lamexp_entry_check_flag != 0x789E09B2) { - FatalAppExit(0, L"Application initialization has failed, take care!"); - TerminateProcess(GetCurrentProcess(), -1); + lamexp_fatal_exit(L"Application initialization has failed, take care!"); } //Zero *before* constructors are called @@ -2345,6 +2302,35 @@ extern "C" } } +/* + * Fatal application exit + */ +#pragma intrinsic(_InterlockedExchange) +void lamexp_fatal_exit(const wchar_t* exitMessage, const wchar_t* errorBoxMessage) +{ + static volatile long bFatalFlag = 0L; + + if(_InterlockedExchange(&bFatalFlag, 1L) == 0L) + { + if(GetCurrentThreadId() != g_main_thread_id) + { + HANDLE mainThread = OpenThread(THREAD_TERMINATE, FALSE, g_main_thread_id); + if(mainThread) TerminateThread(mainThread, ULONG_MAX); + } + + if(errorBoxMessage) + { + MessageBoxW(NULL, errorBoxMessage, L"LameXP - GURU MEDITATION", MB_ICONERROR | MB_TOPMOST | MB_TASKMODAL); + } + + for(;;) + { + FatalAppExit(0, exitMessage); + TerminateProcess(GetCurrentProcess(), -1); + } + } +} + /* * Finalization function (final clean-up) */ diff --git a/src/Global.h b/src/Global.h index 9c4b6307..7c4d626d 100644 --- a/src/Global.h +++ b/src/Global.h @@ -158,6 +158,7 @@ const QString lamexp_clean_filename(const QString &str); const QString lamexp_clean_filepath(const QString &str); void lamexp_seed_rand(void); unsigned int lamexp_rand(void); +void lamexp_fatal_exit(const wchar_t* exitMessage, const wchar_t* errorBoxMessage = NULL); //Debug-only functions SIZE_T lamexp_dbg_private_bytes(void); diff --git a/src/Main.cpp b/src/Main.cpp index 300fc773..dc9e65ea 100644 --- a/src/Main.cpp +++ b/src/Main.cpp @@ -233,24 +233,21 @@ static int _main(int argc, char* argv[]) fflush(stdout); fflush(stderr); fprintf(stderr, "\nGURU MEDITATION !!!\n\nException error message: %s\n", error); - FatalAppExit(0, L"Unhandeled C++ exception error, application will exit!"); - TerminateProcess(GetCurrentProcess(), -1); + lamexp_fatal_exit(L"Unhandeled C++ exception error, application will exit!"); } catch(int error) { fflush(stdout); fflush(stderr); fprintf(stderr, "\nGURU MEDITATION !!!\n\nException error code: 0x%X\n", error); - FatalAppExit(0, L"Unhandeled C++ exception error, application will exit!"); - TerminateProcess(GetCurrentProcess(), -1); + lamexp_fatal_exit(L"Unhandeled C++ exception error, application will exit!"); } catch(...) { fflush(stdout); fflush(stderr); fprintf(stderr, "\nGURU MEDITATION !!!\n"); - FatalAppExit(0, L"Unhandeled C++ exception error, application will exit!"); - TerminateProcess(GetCurrentProcess(), -1); + lamexp_fatal_exit(L"Unhandeled C++ exception error, application will exit!"); } return iResult; } @@ -277,8 +274,7 @@ int main(int argc, char* argv[]) fflush(stdout); fflush(stderr); fprintf(stderr, "\nGURU MEDITATION !!!\n\nUnhandeled structured exception error! [code: 0x%X]\n", GetExceptionCode()); - FatalAppExit(0, L"Unhandeled structured exception error, application will exit!"); - TerminateProcess(GetCurrentProcess(), -1); + lamexp_fatal_exit(L"Unhandeled structured exception error, application will exit!"); } } } diff --git a/src/Thread_CPUObserver.cpp b/src/Thread_CPUObserver.cpp index 8af9619c..6d3460b7 100644 --- a/src/Thread_CPUObserver.cpp +++ b/src/Thread_CPUObserver.cpp @@ -73,8 +73,7 @@ void CPUObserverThread::run(void) fflush(stdout); fflush(stderr); fprintf(stderr, "\nGURU MEDITATION !!!\n"); - FatalAppExit(0, L"Unhandeled exception error, application will exit!"); - TerminateProcess(GetCurrentProcess(), -1); + lamexp_fatal_exit(L"Unhandeled exception error, application will exit!"); } while(m_semaphore.available()) m_semaphore.tryAcquire(); diff --git a/src/Thread_DiskObserver.cpp b/src/Thread_DiskObserver.cpp index d72029f3..cb089426 100644 --- a/src/Thread_DiskObserver.cpp +++ b/src/Thread_DiskObserver.cpp @@ -59,8 +59,7 @@ void DiskObserverThread::run(void) fflush(stdout); fflush(stderr); fprintf(stderr, "\nGURU MEDITATION !!!\n"); - FatalAppExit(0, L"Unhandeled exception error, application will exit!"); - TerminateProcess(GetCurrentProcess(), -1); + lamexp_fatal_exit(L"Unhandeled exception error, application will exit!"); } while(m_semaphore.available()) m_semaphore.tryAcquire(); diff --git a/src/Thread_Process.cpp b/src/Thread_Process.cpp index 159d5bd8..c89643fa 100644 --- a/src/Thread_Process.cpp +++ b/src/Thread_Process.cpp @@ -113,8 +113,7 @@ void ProcessThread::run() fflush(stdout); fflush(stderr); fprintf(stderr, "\nGURU MEDITATION !!!\n"); - FatalAppExit(0, L"Unhandeled exception error, application will exit!"); - TerminateProcess(GetCurrentProcess(), -1); + lamexp_fatal_exit(L"Unhandeled exception error, application will exit!"); } } diff --git a/src/Thread_RAMObserver.cpp b/src/Thread_RAMObserver.cpp index ac8efd40..3885937b 100644 --- a/src/Thread_RAMObserver.cpp +++ b/src/Thread_RAMObserver.cpp @@ -53,8 +53,7 @@ void RAMObserverThread::run(void) fflush(stdout); fflush(stderr); fprintf(stderr, "\nGURU MEDITATION !!!\n"); - FatalAppExit(0, L"Unhandeled exception error, application will exit!"); - TerminateProcess(GetCurrentProcess(), -1); + lamexp_fatal_exit(L"Unhandeled exception error, application will exit!"); } while(m_semaphore.available()) m_semaphore.tryAcquire();