Tutorial :OpenGL suppresses exceptions in MFC dialog-based application



Question:

I have an MFC-driven dialog-based application created with MSVS2005. Here is my problem step by step. I have button on my dialog and corresponding click-handler with code like this:

int* i = 0;  *i = 3;  

I'm running debug version of program and when I click on the button, Visual Studio catches focus and alerts "Access violation writing location" exception, program cannot recover from the error and all I can do is to stop debugging. And this is the right behavior.

Now I add some OpenGL initialization code in the OnInitDialog() method:

    HDC DC = GetDC(GetSafeHwnd());      static PIXELFORMATDESCRIPTOR pfd =      {        sizeof(PIXELFORMATDESCRIPTOR), // size of this pfd        1, // version number        PFD_DRAW_TO_WINDOW | // support window        PFD_SUPPORT_OPENGL | // support OpenGL        PFD_DOUBLEBUFFER, // double buffered        PFD_TYPE_RGBA, // RGBA type        24, // 24-bit color depth        0, 0, 0, 0, 0, 0, // color bits ignored        0, // no alpha buffer        0, // shift bit ignored        0, // no accumulation buffer        0, 0, 0, 0, // accum bits ignored        32, // 32-bit z-buffer        0, // no stencil buffer        0, // no auxiliary buffer        PFD_MAIN_PLANE, // main layer        0, // reserved        0, 0, 0 // layer masks ignored      };        int pixelformat = ChoosePixelFormat(DC, &pfd);      SetPixelFormat(DC, pixelformat, &pfd);        HGLRC hrc = wglCreateContext(DC);      ASSERT(hrc != NULL);      wglMakeCurrent(DC, hrc);  

Of course this is not exactly what I do, it is the simplified version of my code. Well now the strange things begin to happen: all initialization is fine, there are no errors in OnInitDialog(), but when I click the button... no exception is thrown. Nothing happens. At all. If I set a break-point at the *i = 3; and press F11 on it, the handler-function halts immediately and focus is returned to the application, which continue to work well. I can click button again and the same thing will happen.

It seems like someone had handled occurred exception of access violation and silently returned execution into main application message-receiving cycle.

If I comment the line wglMakeCurrent(DC, hrc);, all works fine as before, exception is thrown and Visual Studio catches it and shows window with error message and program must be terminated afterwards.

I experience this problem under Windows 7 64-bit, NVIDIA GeForce 8800 with latest drivers (of 11.01.2010) available at website installed. My colleague has Windows Vista 32-bit and has no such problem - exception is thrown and application crashes in both cases.

Well, hope good guys will help me :)

PS The problem originally where posted under this topic.


Solution:1

Ok, I found out some more information about this. In my case it's windows 7 that installs KiUserCallbackExceptionHandler as exception handler, before calling my WndProc and giving me execution control. This is done by ntdll!KiUserCallbackDispatcher. I suspect that this is a security measure taken by Microsoft to prevent hacking into SEH.

The solution is to wrap your wndproc (or hookproc) with a try/except frame so you can catch the exception before Windows does.

Thanks to Skywing at http://www.nynaeve.net/

We've contacted nVidia about this issue, but they say it's not their bug, but rather the Microsoft's. Could you please tell how you located the exception handler? And do you have some additional information, e.g. some feedbacks from Microsoft?

I used the "!exchain"-command in WinDbg to get this information.


Solution:2

Rather than wrapping the WndProc or hooking all WndProcs, you could use Vectored Exception Handling:

http://msdn.microsoft.com/en-us/library/ms679274.aspx


Solution:3

First, both behaviors are correct. Dereferencing a null pointer is "undefined behavior", not a guaranteed access violation.

First, find out whether this is related to exception throwing or only to accessing memory location zero (try a different exception).

If you configure Visual Studio to stop on first-chance access violations, does it break?

Call VirtualQuery(NULL, ...) before and after glMakeCurrent and compare. Maybe the nVidia OpenGL drivers VirtualAlloc page zero (a bad idea, but not impossible or illegal).


Solution:4

I had a similar behaviour (nVidia's driver installed a vectored exception handler that ultimately lead to a crash), see: https://stackoverflow.com/questions/4876796/why-does-an-exception-handled-by-nvidias-opengl-driver-in-copydown-memmove-late

It went away when installing the latest nVidia driver (Jan 2011).


Solution:5

I found this question when I was looking at a similar problem. Our problem turned out to be silent consumption of exceptions when running a 32-bit application on 64-bit Windows.

http://connect.microsoft.com/VisualStudio/feedback/details/550944/hardware-exceptions-on-x64-machines-are-silently-caught-in-wndproc-messages

There’s a fix available from Microsoft, though deploying it is somewhat challenging if you have multiple target platforms:

http://support.microsoft.com/kb/976038

Here's an article on the subject describing the behavior:

http://blog.paulbetts.org/index.php/2010/07/20/the-case-of-the-disappearing-onload-exception-user-mode-callback-exceptions-in-x64/

This thread on stack overflow also describes the problem I was experiencing: Exceptions silently caught by Windows, how to handle manually?


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »