views:

331

answers:

3

Hello.

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.

A: 

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).

Ben Voigt
Same behavior for any exceptions, including manually raised with throw operator. If I configure Visual Studio to stop on first-chance exceptions, it breaks.
Mikhail
Sounds like a vectored exception handler is registered and eats the exception. http://msdn.microsoft.com/en-us/library/ms681420(VS.85).aspxBreak at the first-chance exception, then start single-stepping (make sure you have the option "Just My Code" disabled so that you can step through other libraries). Probably some handler returns EXCEPTION_CONTINUE_EXECUTION instead of EXCEPTION_CONTINUE_SEARCH.
Ben Voigt
Compared results of VirtualQuery(NULL, ...), results are the same for calls before and after wglCreateContext().
Mikhail
The VirtualQuery assumption was for troubleshooting if NULL pointer exceptions weren't triggering but other exceptions were. Since you have all exceptions being swallowed, step through vectored exception handlers. You should at least get some insight into what DLLs contain the handlers and why the behavior is different between your two systems.
Ben Voigt
+1  A: 

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.

Magnus Skog
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?
Mikhail
A: 

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

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

Mark Ingram