Sign in to follow this  
Damocles

wglDeleteContext exception error

Recommended Posts

Damocles    133
I'm having a very strange problem here. After much head scratching and hair pulling, I finally found the source of a long standing exception error in my program - but I have no idea how it is happening. My code to release the context is straight out of Nehe's framework...
if (hRC)											// Do We Have A Rendering Context?
	{
		if (!wglMakeCurrent(NULL,NULL))					// Are We Able To Release The DC And RC Contexts?
		{
			MessageBox(NULL,"Release Of DC And RC Failed.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);
		}

		if (!wglDeleteContext(hRC))						// Are We Able To Delete The RC?
		{
			MessageBox(NULL,"Release Rendering Context Failed.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);
		}
		hRC=NULL;										// Set RC To NULL
	}
the problem is, when the program reaches the wglDeleteContext function call it bombs with an access violation error. I have checked, and the handle being passed in is definitely the one created by the wglCreateContext function at the start of my program. There is only one context and one window in my program. I don't understand why it is crashing here. The context is there, it has been released before the delete call. I simply cannot see why this is going wrong. Can someone enlighten me as to potential causes for this error? What else might cause the context delete to fail so badly?

Share this post


Link to post
Share on other sites
Damocles    133
So no-one has any ideas what's causing it? The only drawing operations I'm using are glDrawArrays, glColor and various matrix transformations.

Some other bizarre behaviour has started appearing too, like textures being deleted from video memory without any calls to that effect.

This is really driving me nuts - I can't finish the program if it's going to crash every time someone tries to exit.

Any ideas, no matter how far fetched, would be really appreciated.

Share this post


Link to post
Share on other sites
Dredge-Master    175
you will probably have to post more code.

ie; how you are acquiring hrc (the rendering context).
If it isn't the correct pointer, it will bomb out.
This can happen when HRC is set to something that isn't the correct adress.
In your code it will still check.



why don't you try something like this?


HGLRC hglrc;
HDC hdc;
if ( hglrc = wglGetCurrentContext() )
{
hdc = wglGetCurrentDC();
wglMakeCurrent( NULL, NULL );
ReleaseDC( hwnd, hdc );
wglDeleteContext( hglrc );
}


or this for less code
HGLRC hglrc;
if ( hglrc = wglGetCurrentContext() )
{
wglMakeCurrent( NULL, NULL );
wglDeleteContext( hglrc );
}


For the record, I use the second version (the cut down one) as I use VCL in a custom class I call glCore.


PS: If it works, please rate me. If it doesn't work, then pelase don't rate me. :)

Share this post


Link to post
Share on other sites
Damocles    133
Nope, they don't work either :( I acquire my hrc with:


if (!(hDC=GetDC(hWnd)))
{
// graceful exit
}
if (!(hRC=wglCreateContext(hDC)))
{
// graceful exit
}



This was all working fine for months. Then for seemingly no reason it started crashing on exit. Debugging leads me as far as the wglDeleteContext call, so I can only assume that the context is undeletable for some reason. Some reason so serious that it causes the program to crash. I don't do anything that would tie up the context though. It's a single threaded program, with one window, one context and no extensions used. Drawing is limited to glDrawArrays and other basic GL functions. I don't use display lists or begin/end pairs. I just cannot think of anything that might be tying up the context like that. Nor can I think of any other reason why it might bomb on context delete :(

And as I said in the beginning, the hrc handle is definitely the correct value. I followed it from creation to deletion and it doesn't change.

Share this post


Link to post
Share on other sites
Damocles    133
I looked through the SDL code and it does exactly the same as I have been doing and as has been suggested above. I doubt the problem is in the actual exit code, I think the problem lies elsewhere, I just have no idea what might be generating a situation like this. I need some ides of the sort of situations that might result in a failure to delete the context.

Share this post


Link to post
Share on other sites
edwinnie    122
HI i am experiencing this problem for quite some time as well.
i first noticed it was when i fire up a D3D application in debug mode, then shutting it down. Then if i fire up the opengl app, and try to shutdown, it will crash on the deletecontext(), apparently i think ATI drivers are responsible for this problem...

THX!
Edwinz

Share this post


Link to post
Share on other sites
Damocles    133
Not unless the same problem is in the NVidia drivers too. I don't have ATI on my system. I've searched through this forum and other forums, and the general consensus seems to be that the context gets tied up with certain operations and cannot be deleted until these operations are completed/exited. Alas, I cannot find what might be causing it in my code. I don't use any operations that "tie up" openGL.

Does anyone know of any commands that could check if the context is in use, or perhaps force the context to stop being used?

Share this post


Link to post
Share on other sites
Dredge-Master    175
you could always try this before your destruction code

glFinish();
glFlush();


That should tell the que to go and spit everything out of the buffer. I have never accounted your problem (since it wasn't due to an invalid context like I originally assumed) so I can't tell.

Share this post


Link to post
Share on other sites
Damocles    133
DM - the finish and flush idea doesn't work :(

Magmai - do you have any info on how best to catch these elusive non-fatal exceptions? I've tried setting VC to stop on every known exception during debug tests, but it still doesn't find anything. I've tried the MS debug memory leak checkers and another memory leak checker library to try and ensure the code is safe there, and neither of them show up any faults.

I tried simply adding the exit point at the end of the veru first cycle through the game (EG, it draws the main menu for one frame then quits) and the error does NOT show up then. However, if the game is allowed to run for several seconds and exit normally, then the crash appears. So I've narrowed down the area where the exceptions may be, but I can't find anything wrong in that code.

I tried looking through the opengl.org forums, and it seems several people have had this problem and not one person has managed to find a solution or even a cause :(

Share this post


Link to post
Share on other sites
Damocles    133
I eventually managed to track down and remove the error by disabling various parts of the program and seeing if the error remained. After a few hours, I managed to whittle it down to a call to render a specific texture. The texture was a simple 4x4 white block of pixels. I removed it and let opengl use it's standard white texture that represents no texture available instead, and the program works fine now.

I have no idea what was causing the error, I can only figure there was some kind of strange corruption in the texture file.

Anyone else suffering from this might want to try disabling various texture rendering calls to see if it is the cause or not.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this