Wanting2LearnMan
asked on
CreateCompatibleDC and Memory leak question
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_MYBITMA P);
bmDC.CreateCompatibleDC(&d c);
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?
I have an OnPaint function as follows:
void CMyClass::OnPaint()
{
CBitmap bmp;
BITMAP bi;
CClientDC dc(this);
CDC bmDC;
bmp.LoadBitmap(IDB_MYBITMA
bmDC.CreateCompatibleDC(&d
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?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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.
I am using eVC++ so debugging my app is tricky.
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);
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);
ASKER
>>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.
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.
ASKER
Oh and yes its in the OnPaint() function
ASKER
ONe more question,
Do I need to do:
bmp.DeleteObject();
for the CBitmap bmp?
Thanks
Do I need to do:
bmp.DeleteObject();
for the CBitmap bmp?
Thanks
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.
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.
ASKER