tags:

views:

186

answers:

3

Sometimes in my OpenGL application I get an access violation in the following API call:

wglMakeCurrent(NULL, NULL);

The application only has one single thread, and I've checked that before that call, both the DC and HGLRC that are currently used are correct and valid.

There are three different windows with OpenGL content, and they're all redrawn on WM_PAINT messages and if a refresh is required due to user interaction (e.g., picking an object).

Also this access violation happens on different machines with different graphic cards, so I don't think it's a driver issue.

What could make this API call crash? What should I investigate in the app code to find out where/why this happens? I'm really lost here since I've checked everything I could think of already. I hope someone can give me hints/ideas on what more to check.

A: 

Try adding a glFlush call just before you do this. Perhaps the rendering pipeline has not yet been fully flushed to the GPU.

Tarydon
+1  A: 

Is the purpose of this call to release the current thread's device context? That is the only time passing NULL for both parameters is valid. From the documentation:

"If hglrc is NULL, the function makes the calling thread's current rendering context no longer current, and releases the device context that is used by the rendering context. In this case, hdc is ignored."

Further to Tarydon's suggestion of adding a glFlush, it appears this would be redundant:

"Before switching to the new rendering context, OpenGL flushes any previous rendering context that was current to the calling thread."

Given the API actually crashes, and does not simply result in a failure, the only suggestion I can think of is that the thread on which you are calling wglMakeCurrent to release the HGLRC is not the same thread context used to associate the HGLRC with that device context. So the driver may be looking up the wrong thread-local storage for the call, thus causing the crash.

What happens if you add some logging to your code and print the current thread ID just before each call to wglMakeCurrent?

See:

gavinb
As I mentioned, the whole app only uses one thread, so problems with different threads are not possible.And adding logging code doesn't help either: the app really hates me and simply doesn't crash when I add logging code. I guess I have to dig here very very deep in ten year old code :(
Stefan
@Stefan Next I would suggest adding logging code to all your `wglMakeCurrent` calls and displaying the values of `DC` and `HGLRC`, and its return value, making sure the calls are consistent. You could also try different forms of logging (eg. writing to a file, or using OutputDebugString, etc). In the past, I've found that if your app changes behaviour when adding (or removing) logging code, this can be a symptom of memory corruption. You might want to try a tool such as BoundsChecker, which can detect errors at runtime (memory corruption as well as API failures, etc).
gavinb
A: 

Turns out that an updated graphics driver fixed the issue. So it was a problem in the driver.

Stefan