From f32476d63b395c2d844247bd4d7fae4173df4065 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Jos=C3=A9=20Fonseca?= Date: Wed, 24 Aug 2011 16:25:15 +0100 Subject: [PATCH] Fix cleanup Windows' CatchInterrupts implementation. --- os_win32.cpp | 47 +++++++++++++++++++++-------------------------- 1 file changed, 21 insertions(+), 26 deletions(-) diff --git a/os_win32.cpp b/os_win32.cpp index 4bfdf5e..a35e2a3 100644 --- a/os_win32.cpp +++ b/os_win32.cpp @@ -24,6 +24,7 @@ **************************************************************************/ #include +#include #include #include #include @@ -135,48 +136,42 @@ Abort(void) } -struct Interrupts -{ - Interrupts() - : set(false), - prevfilter(NULL), - handler(NULL) - {} - - bool set; - LPTOP_LEVEL_EXCEPTION_FILTER prevFilter; +static LPTOP_LEVEL_EXCEPTION_FILTER prevExceptionFilter = NULL; - void (*handler)(int); -}; -static Interrupts interrupts; +static void (*handler)(int) = NULL; -LONG WINAPI InterruptHandler(EXCEPTION_POINTERS *exceptionInfo) +static LONG WINAPI UnhandledExceptionFilter(PEXCEPTION_POINTERS pExceptionInfo) { - if (interrupts.handler) { + if (handler) { int exceptionCode = 0; - if (exceptionInfo) { - exceptionCode = exceptionInfo->ExceptionRecord.ExceptionCode; + if (pExceptionInfo) { + PEXCEPTION_RECORD pExceptionRecord = pExceptionInfo->ExceptionRecord; + if (pExceptionRecord) { + exceptionCode = pExceptionRecord->ExceptionCode; + } } - interrupts.handler(exceptionCode); + handler(exceptionCode); } - if (interrupts.prevFilter) { - return interrupts.prevFilter(exceptionInfo); + if (prevExceptionFilter) { + return prevExceptionFilter(pExceptionInfo); } else { - return EXCEPTION_CONTINUE_SEARCH; + return EXCEPTION_CONTINUE_SEARCH; } } void CatchInterrupts(void (*func)(int)) { - interrupts.handler = func; + assert(!handler); + assert(!prevExceptionFilter); + + handler = func; - if (!interrupts.set) { - interrupts.prevFilter = - SetUnhandledExceptionFilter(InterruptHandler); - interrupts.set = true; + if (handler && !prevExceptionFilter) { + prevExceptionFilter = + SetUnhandledExceptionFilter(UnhandledExceptionFilter); } } -- 2.45.2