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 wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

GetDC() is leaking memory?

Status
Not open for further replies.

timmay3141

Programmer
Dec 3, 2002
468
US
In a program I'm working on (the same program that was causing me trouble a couple months ago that I posted about then, if you remember), I have a thread similar to this. It does more, but I have left out the unimportant parts:

UINT MyViewClass::HandleThread(LPVOID lp)
{
MyViewClass* pView = (MyViewClass*)lp;
MyDocClass* pDoc = pView->GetDocument();

while(1)
{
CDC* pDC = pView->GetDC(); // Line 8
pDoc->DrawStuff(pDC); // Line 9
::Sleep(100);
}

return 0;
}

Using the Stress Utility, I've managed to find the source of the problems. When I leave it as is, the GDI32 drops drastically, along with the User % and to a lesser extent the GDI%. When I comment out line 9, it doesn't help at all. However, when I comment out the lines 8 and 9, I don't lose anything. I took this to mean that the repeated GetDC calls are reaking havoc on my system (it lags really bad after a while). What is the problem, and what can I do to fix it?
 
remove line 8 and don't pass a CDC* to DrawStuff(). Instead, obtain the CDC* within the DrawStuff() function and perform the necessary clean ups there.
tellis.gif

programmer (prog'ram'er), n A hot-headed, anorak wearing, pimple-faced computer geek.
 
Do you restore the dc when your done? When you select an object into the dc, it will return a pointer to it's base object. If you dont re-select that back into the DC you will have leaks (99% sure of that cause I think it bit our butt here at work)

Matt
 
Like Matt said, those GDI based objects are notorious for causing memory leaks!
tellis.gif

programmer (prog'ram'er), n A hot-headed, anorak wearing, pimple-faced computer geek.
 
From MSDN's entry for ReleaseDC:

"The application must call the ReleaseDC member function for each call to the GetWindowDC member function and for each call to the GetDC member function."

So I recommend you add

pView->ReleaseDC(pDC);

after line 9.

I don't know if that is the source of your problem, but it can't hurt to try :)

Vincent
 
Thanks everyone for your responses. I'll fix it tomorrow and get back to you.
 
The ReleaseDC() fixed it. Thanks for your advice, everyone.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top