Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How Important is it to release a DC? 1

Status
Not open for further replies.

Zyrenthian

Programmer
Mar 30, 2001
1,440
0
0
US
Well, I never thought it was THAT important, it is only an itty bitty little dc right? WRONG!

Our software does a lot of image processing. As a result, we need a DC to do a lot of our displaying to the screen. Images need to be resized, color adjusted, enhanced etc... I have been guilty of forgetting to restore my DC to its original format and of not releasing my DC. When you select an object into a DC, it returns a pointer to what used to be there. For example, if I have a palette and a pointer to a dc I used to just do the following.

Code:
CDC* pDC = GetDC();
pDC->SelectPalette(myPalette);
// code
// end of function

I have come to find out that where I did this it was un-noticable for the most part. My function was only called when the user clicked copy to clipboard. We never noticed the problem because, though it happens, it does not happen ALL the time. What I should have done in the code is

Code:
CDC* pDC = GetDC();
CPalette* pPalette = NULL;
pPalette = pDC->SelectPalette(myPalette);
...
// at the end of the funciton
// restore the DC to how it used to be
pDC->SelectPalette(pPalette);
ReleaseDC(pDC);

Now, unless you are doing a lot of work with DCs you will most likely never see a problem. It took us 4 hours of looping images to even find this problem and even then the resource handle leak was SO bad, part of the error message was truncated. I guess after not releasing over 2000 DC's, the computer doesnt like you anymore and craps out... GO FIGURE :p.

So how can you figure out if you have a resource handle leak problem? In our case it was easy, we just looped images and watched our GDI continually INCREASE. In the task manager, click on the View menu and choose Select Columns. Check of the GDI Object check box. Run your app and watch it on the Processes tab.

As I said, if you dont do A LOT of things with DCs and dont release them, this problem will most likely never been seen. A worst case scenario for our image loops would have had 16 DCs created a second and not released. All of this was running in threads. Then the whole thing repeats itself the next second.

So ladies and gentlemen, the reason I post this is because the debugger does not catch it. Until, I put code in for better memory management, we were actually seeing an "Out of Memory" error on this instead of the "A required resource was unavialable" message. The problem with this was so bad that "new" was throwing the exception. Contrary to popular belief, new does not return null when it fails. Instead an exception is thrown.

"try" it some time and I am sure you will "catch" a glimpse of what happens when you have no memory to allocate. LOL

Ok, bad puns asside. Please pay attention to this if you ever begin working with DCs... it might just save you the 2 month headache I and my co-workers have had.

Matt
 
See my post on April 15, thread 713-251622. I never got an answer - but answered it myself on May 6. As an old timer I still don't understand why Microsoft made a simple thing so complicated.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top