Solved

CreateCompatibleDC and Memory leak question

Posted on 2009-05-05
11
872 Views
Last Modified: 2013-12-21
I am investigating a memory leak in my Windows Mobile application (Program just freezes from time to time).  I have read that when you no longer need a memory device context (such as CreateCompatibleDC) then  call the DeleteDC function to delete it.

I have an OnPaint function as follows:
void CMyClass::OnPaint()
{
      CBitmap bmp;
       BITMAP bi;
       CClientDC dc(this);
       CDC bmDC;

       bmp.LoadBitmap(IDB_MYBITMAP);
       bmDC.CreateCompatibleDC(&dc);
       CBitmap *pOldbmp = bmDC.SelectObject(&bmp);
       bmp.GetBitmap(&bi);
       dc.BitBlt(xPos, yPos, bi.bmWidth, bi.bmHeight, &bmDC, 0, 0, SRCCOPY);

}


I do not have any DeleteDC function here, where should it go and do you think the above could cause my app to crash due to memory leak over a period of time?
0
Comment
Question by:Wanting2LearnMan
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 2
  • 2
  • +2
11 Comments
 
LVL 31

Accepted Solution

by:
Zoppo earned 300 total points
ID: 24304241
Hi Wanting2LearnMan,

you need to select the 'old' bitmap back into the DC to avoid GDI leaks, so just after the 'BitBlt' call 'bmDC.SelectObject( pOldbmp );'.

The 'bmp' and 'cmDC' then should be release correctly with there destructors getting called.

Hope that helps,

ZOPPO
0
 
LVL 19

Assisted Solution

by:alb66
alb66 earned 100 total points
ID: 24304242
DeleteDC should be automatically called by the CDC destructor so, in my opinion, it is not the problem.
0
 
LVL 40

Assisted Solution

by:evilrix
evilrix earned 100 total points
ID: 24304251
>> do you think the above could cause my app to crash due to memory leak
Unless it's running out of memory and being terminated by the OS, it's very rare for an app to crash just due to a leak. Can you clarify specifically what you mean by "crash"?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 31

Assisted Solution

by:Zoppo
Zoppo earned 300 total points
ID: 24304274
> Unless it's running out of memory and being terminated by the OS, it's very rare for an app to crash just due to a leak

With memory leaks you're right, but GDI leaks can cause quite strange effects, even the OS may crash or behave strange since GDI resources are system-wide restricted.

ZOPPO
0
 

Author Comment

by:Wanting2LearnMan
ID: 24304413
Thanks guys.  Users of my app have told me that the program just 'froze' on them when they were using it.  Out of about 25 users 5 have told me this.  They have told me that they were 'just using it' and have not been able to say exactly the steps they took.  So I'm, tryin to look at all possibilities going through my code.
0
 

Author Comment

by:Wanting2LearnMan
ID: 24304919
Is there any way of monotoring in code how much free memory an app has to use.  If so then I could output the available memory at various stages and see if memory is getting leaked anywhere.

I am using eVC++ so debugging my app is tricky.
0
 
LVL 33

Expert Comment

by:pgnatyuk
ID: 24313734
The effect with your is exactly the GDI leak.
After BitBlt you have to select the old HBITMAP and delete DC you created. Something like that:
tmDC.SelectObject(pOldbmp);
bmDC.DeleteDC();
I don't understand why you need CClientDC? And CDC. You are in OnPaint. You should you CPaintDC only.
I'm afraid the MFC confuses not only you in OnPaint. Without MFC it looks simple:
PAINTSTRUCT paint = {0};
HDC hDC = BeginPaint(hWnd, &paint);
//Here is your code drawing on this hDC
EndPaint(hWnd, &paint);
0
 

Author Comment

by:Wanting2LearnMan
ID: 24318814
>>The effect with your is exactly the GDI leak.
What do yo umean by this?  Do you mean that you think this is the reason my app is 'freezing' thanks.

>>I don't understand why you need CClientDC? And CDC. You are in OnPaint. You should you CPaintDC only.
I wrote this about 2 years ago, I think I copied it off another example code, I'm not an expert on MFC drawing so I may be wrong to use this.
0
 

Author Comment

by:Wanting2LearnMan
ID: 24318921
Oh and yes its in the OnPaint() function
0
 

Author Comment

by:Wanting2LearnMan
ID: 24319080
ONe more question,

Do I need to do:
bmp.DeleteObject();

for the CBitmap bmp?

Thanks
0
 
LVL 33

Expert Comment

by:pgnatyuk
ID: 24322346
I'd prefer to call DeleteObject. I don't know if it happens from the destructor.
With GDI I prefer to set everything explicitly.
You may load this bitmap only once when you create your object and delete it whe you are destroying the object. If you will not see your picture - you have a leak - probably forgot to select the OldBitmap after the drawing
bmDC.SelectObject(pOldbmp);
only after this line you can delete your bitmap and DC.
GDI object selected in a DC is not deleted as well as the DC.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

With Windows Embedded Handheld, called Windows Mobile, Microsoft re-designed the user interface. The Start Icon moved down to the bottom, inside the menu bar area.   If you need to hide the Start Icon and/or the SIP (soft input panel, softwar…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
Suggested Courses

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question