I’ve been tracking down a really insidious bug at work. The event that seems to cause the very strange behavior I’ve been tracking down appears to be an exception being thrown while processing a timer callback. The exception is NOT handled by any of my code, therefore I would expect the debugger to be notified of the unhandled exception and alert me with a nice obnoxous pop-up. No, instead the “first chance” exception message traces out to the debugger and everything silently moves on.
I’ve written up the following program that demonstrates this issue:
#include "stdafx.h"
#include <Windows.h>
class FooExcept
{
};
VOID CALLBACK Timer(HWND hwnd,
UINT uMsg,
UINT_PTR idEvent,
DWORD dwTime)
{
printf("Here\n");
throw FooExcept();
printf("Also Here\n");
}
int _tmain(int argc, _TCHAR* argv[])
{
SetTimer(0, 0, 1000, Timer);
int bRet;
HWND hWnd;
MSG msg;
// Standard Win32 message pump
while( (bRet = GetMessage( &msg, NULL, 0, 0 )) != 0)
{
if (bRet == -1)
{
// handle the error and possibly exit
}
else
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return 0;
}
The output of this program to the Visual Studio Output window is this repeated:
First-chance exception at 0x76dbb9bc in TimerTest.exe: Microsoft C++ exception: FooExcept at memory location 0x0034f743..
Of course my project is MUCH more complicated and is printing out a lot of TRACE information to the debugger. Its easy for first-chance exception to get lost in the output.
HOWEVER
That’s not my concern. This is clearly an exception Im not handling. Is Windows surreptitiously deciding to handle this for me? Why? If not, why isn’t it giving me a blatant pop-up alerting me of the unhandled exception? Such a pop-up could have saved me days of debugging.
This behavior seems consistent between XP/VS 2008 and Win7/VS 2010.
This has been been fixed, to some degree (see below), in Windows 7 SP1.
In article http://support.microsoft.com/kb/976038 you will find mention of a bugfix and explanation how to use it to disable this behaviour. You have to either:
SetProcessUserModeExceptionPolicyexported bykernel32.dllI have verified in Updates in Win7 and WS08R2 SP1.xls that this bugfix is indeed included in Windows 7 Service Pack 1, so all you have to do is to enable it (assuming you are lucky enough to have Windows 7 SP1 already installed).
As for explanation why this has been done, well we can just guess some ill-advised attempt to prevent legacy application from crashing in the name of backwards compatibility.