mfc >> GDI OnPaint DCs and memory leaks

by Torsten Curdt » Fri, 28 Nov 2003 05:01:46 GMT

I am tracking down a memory leak. So I wanna make sure
I got everything right...

I have an AbstractDialog class with

void AbstractDialog::OnPaint()
{
CPaintDC dc(this);
OnDraw(dc);
}

void AbstractDialog::OnDraw( CPaintDC& dc )
{
}

and each Dialog can now easily override the OnDraw method.
AFAIU overriding OnPaint() is not a good idea if you have
a deeper inheritance because the paint message should only
be sent once. Now in my dialog class I have e.g.:

void MyDialog::OnDraw( CPaintDC& dc )
{
CDC memDC;
memDC.CreateCompatibleDC(&dc);
...
memDC.DeleteDC();
}

IIUC omitting the DeleteDC() call would give a GDI memory
leak, right? ...but the CPaintDC will be deleted from it's
destructor, right?


mfc >> GDI OnPaint DCs and memory leaks

by Jeff Partch [MVP] » Fri, 28 Nov 2003 07:06:58 GMT






The dtors for both objects should do the right thing: CPaintDC should call
EndPaint and CDC should DeleteDC.
--
Jeff Partch [VC++ MVP]





mfc >> GDI OnPaint DCs and memory leaks

by Torsten Curdt » Fri, 28 Nov 2003 07:23:11 GMT

>>IIUC omitting the DeleteDC() call would give a GDI memory

That's strange :-/ ...I was came across a resource leak in
my application because DeleteDC calls were missing.

I added them and then found a 1:1 ratio of create/delete
calls. I am currently running a test in release mode.
But that will take a few hours... tomorrow we'll see

Talk to you tomorrow. Thanks!


GDI OnPaint DCs and memory leaks

by aman_zafar » Fri, 28 Nov 2003 14:02:57 GMT

why do you want to do like that since ur OnPaint Handler will call the
Destuctor so what the need to do it, don't delete it.


GDI OnPaint DCs and memory leaks

by Torsten Curdt » Fri, 28 Nov 2003 17:18:24 GMT





Ok .. you were right that wasn't the problem :-/
Seems like the problem only appears in the release
version. But I cannot see why. Here is the code
that must do something terribly wrong:

void AbstractDialog::DrawBitmap( CDC* pDc, int pX, int pY, CBitmap*
pBitmap )
{
CDC memDC;
memDC.CreateCompatibleDC(pDc);

CBitmap* pOld = memDC.SelectObject(pBitmap);
BITMAP bm;
pBitmap->GetObject(sizeof(bm), &bm);
CSize size = pBitmap->SetBitmapDimension(bm.bmWidth, bm.bmHeight);
pDc->BitBlt(pX, pY, bm.bmWidth, bm.bmHeight, &memDC, 0, 0, SRCCOPY);
pDc->SelectObject(pOld);
}

Can someone explain?


Similar Threads

1. FromHandle purpose with DCs and GDI objects

I was wondering what could be the purpose of maintaning a handle map
for these objects. I can't find any advantages of this approach. I got
some ideas but by looking the MFC source code  I found out that all my
ideas doesn't seem to be the answer I'm looking for. Here are the ideas
I got but aren't good:

1- To make sure that a GDI object is deleted only once.
This is correct except that the object will be deleted when the first
MFC object is destroyed hence all other MFC objects refering to the
same GDI object will refer to an invalid handle and I suspect that this
behavior is the source of many bugs in MFC application in
devellopement. Also, the same result could have been achieved by
offering a constructor accepting a HGDIOBJ and setting a BOOL to false
indicating to the object destructor must not delete the underlying GDI
object. So I was wondering what is the plus value of the handle map in
this case. I can't see it. The same reasonning applies to DCs.

Thank you,

2. TreeView checkbox gdi memory leak - CSharp/C#

3. Preventing memory and resource leaks with GDI Objects ???

I am have built a general purpose bitmap/image handling 
class and want to add TextOut() capability to this class. To 
do this I must convert some of my GDI local function 
variables into GDI object member variables. This means that 
I must be able to re-use several GDI objects, instead of 
constructing them and destroying them after a single use.

What issues are raised with resource and memory leaks by 
using CDC and CBitmap objects?  How are these issues 
mitigated? 


4. GDI memory leaks - VC MFC

5. GDI objects and memory leaks

Apologies if this is the wrong group.

How does one go about stopping memory leaks? I'm using XP with VS 7.1 (C++ 
standard). The app I have uses a dialog box as it's gui. Using Task Manager, 
I notice the GDI objects count getting higher and higher everytime I close 
and re-open the dialog box. Is this normal? I've gone through the code and 
made sure I've deleted objects but the count keeps getting higher. Is there 
software that would tell me what the objects are? Or is there another way? 


6. Deleting DCs to avoid resource leak

7. How to detect memory leak and detect who causes a memory leak

8. GDI Onpaint Problem/Question in Custom control - CSharp/C#