• Advertisement
Sign in to follow this  

ReleaseDC, weird error

This topic is 3294 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

Ok so heres the thing i created a canvas class that i can pass instances of my pen, brush and font classes to for drawing/text output but ive recently noticed that if i try to release the DC after the main loop the DC never gets released!! Now this problem isnt due to my class, hWnd or the hDC being invalid as ive tested them numerous ways, such as; stepping through break-points and checking values, wrapping the ReleaseDC in an if statement that checks for validity of the hwnd and hDC and also grabbing the DC outside of the class and releasing outside of the class (after the main loop). There is no problems with releasing the DC before or during the main loop, just after. What is happening here? If you need to see some code please ask which piece i should post as im totally lost as to the where abouts of the problem. Thanks in advance ~Reegan

Share this post


Link to post
Share on other sites
Advertisement
Can you post your entire application loop, starting with the declaration of the HDC variable and moving through the end of the loop to where you want to release it? Without context it's pretty hard to guess what might be going wrong.

Also, one thing to note is that you should use ReleaseDC for system-allocated DCs, and DeleteDC for a DC you created yourself (e.g. using CreateDC).

Share this post


Link to post
Share on other sites
Ok, and yes im using GetDC() to retrieve a DC for my canvas class, anyways heres the code thats involved:

App loop with retrieved dc and released dc:
//test code
HDC hdc = GetDC(Wnd.GetHandle());

while (Wnd.ProcessMessages(true) && !GetAsyncKeyState(VK_ESCAPE))
{

}

if (!ReleaseDC(Wnd.GetHandle(), hdc)) {
std::cout << "Error occurred releasing DC." << std::endl;
Sleep(1000);
}




code for Wnd.ProcesseMessages(true):
bool cWinBase::ProcessMessages(bool bCauseQuit)
{
if (::PeekMessage(&m_Msg, NULL, 0, 0, PM_REMOVE))
{
::TranslateMessage(&m_Msg);
::DispatchMessage(&m_Msg);
}
if (bCauseQuit && m_Msg.message == WM_QUIT) return false;
else return true;
}





thanks :)

Share this post


Link to post
Share on other sites
It's been a while since I've done this so I may be off base, but this is my guess - what's going on is that the window is being destroyed before the end of the message pump. This is normal and to be expected.

However, once you exit the loop, neither the HWND nor its HDC are valid, because the window has been destroyed and its resources reclaimed by the OS.

My suggestion would be to add a case to the window's message handler, trap WM_DESTROY, and when that message arrives call a function in your main file that releases the DC. That should take care of it [smile]

Share this post


Link to post
Share on other sites
Ah, well, that works, but its not really the way i would like to do handle releasing the DC.. guess i have no choice..

Thanks for your help though, i can rest easier knowing the problem in my code now, haha :)

Share this post


Link to post
Share on other sites
Well, you could always write a simple RAII wrapper class that takes an HDC as the sole parameter to its constructor, and then in the destructor call ReleaseDC().

You still need to control the object so that it either goes out of scope or is deleted to make sure the destructor runs and ReleaseDC() is called at the right time, but it'd make things a tiny bit cleaner.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement