Archived

This topic is now archived and is closed to further replies.

HDC not able to be release

This topic is 5585 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am doing some OpenGl work and having some problem with releasing the HDC after wards. I ''ll try to explain it as clearly as I can. Simple basic Opengl that initialise a window - thats all. This is the code that initialises the HDC, pfd is my PIXELFORMATDESCRIPTOR: mhDC = GetDC( mhWindow ); if( !mhDC ) return fatalError( "Fail to create a GL Device Context." ); pixelFormat = ChoosePixelFormat( mhDC,&pfd ); if( !pixelFormat ) return fatalError( "Fail to find a suitable pixel format." ); if( !SetPixelFormat( mhDC,pixelFormat,&pfd )) return fatalError( "Fail to set pixel format." ); mhRC = wglCreateContext( mhDC ); if( !mhRC ) return fatalError( "Fail to create a GL Rendering Context." ); if( !wglMakeCurrent( mhDC,mhRC )) return fatalError( "Fail to activate GL Rendering Context." ); This is the code that cleans the memory on destruction: if( mhRC ) { if( !wglMakeCurrent( NULL,NULL )) MessageBox( NULL,"Fail to release RC and DC.","ERROR",MB_OK|MB_ICONEXCLAMATION ); if( !wglDeleteContext( mhRC )) MessageBox( NULL,"Fail to release Rendering Context.","ERROR",MB_OK|MB_ICONEXCLAMATION ); mhRC = NULL; } if( mhDC && !ReleaseDC( mhWindow,mhDC )) { MessageBox( NULL,"Fail to release Device Context.","ERROR",MB_OK|MB_ICONEXCLAMATION ); mhDC = NULL; } The ReleaseDC(...) method always return false for some reason and I have no idea why. If you need more info I can always provide.

Share this post


Link to post
Share on other sites
I''m not really sure, but maybe it''s because you''re using the DC for GL rendering? Take a look at CS_OWNDC flag for WNDCLASS.style. With it you can call GetDC as much as you want, you''ll always get the same DC, and you never have to release it.

Share this post


Link to post
Share on other sites
The thing is this is parts of the code uses by Nehe tutorial''s, and it does seem to work when I use his code I downloaded.

Thats why I think its wierd and funny why it doesnt work and I have no idea why cause it is the basic coding. So I guess why I am wondering what could stop ReleaseDC(...) from releasing the HDC.

Think I should post this in the General Programming forum?
Anyone any other ideas?

Share this post


Link to post
Share on other sites
I''m not an advanced OpenGL programmer myself but I''ve read into both Nehe''s guides as the OpenGL programming guide and encountered the same exact problems when trying to run my own "engine".

Well, I don''t know if you''ve solved it already but I accidentally found out that after you''ve deleted your Rendering Context, you can''t release the DevContext anymore, for one reason or another.

Nehe''s programs seem to work, yes, but when you release the DC after deleting the RC, everything goes fine as well (on my pc for now).

Now my code in this particular section looks like this:

if (g_hRC)
{
if (!wglMakeCurrent(g_hDC,NULL))
ErrorMessage(3);

if (g_hDC && !ReleaseDC(g_hWnd,g_hDC))
{
ErrorMessage(-1);
g_hDC=NULL;
}

if (!wglDeleteContext(g_hRC))
ErrorMessage(3);

g_hRC = NULL;
}

Any intelligent insights on this matter?

Share this post


Link to post
Share on other sites