Reputation: 108
whenever I execute the below code, my memory in task manager for the application keeps on increasing endlessly. I found similiar questions here on stackoverflow and I did some DeleteObject calls like they stated but this still did not solve the ever increasing memory when this code executes.
How can this be solved? What am I doing wrong?
SetControlPicture(const UINT ID_PICTURE_CONTROL)
{
CImage image;
CBitmap bitmap;
HRESULT hresult;
CStatic* pItem = (CStatic*)GetDlgItem(ID_PICTURE_CONTROL);
hresult = image.Load(_T("./Data/Images/RED_ON.png"));
if(hresult != E_FAIL)
{
HBITMAP hBitMap = image.Detach();
bitmap.Attach(hBitMap);
HBITMAP hBitMapPrev = pItem->SetBitmap(bitmap);
if (hBitMapPrev)
{
DeleteObject(hBitMapPrev); // *** do not forget to delete the previously associated bitmap
}
DeleteObject(hBitMap);
}
}
Upvotes: 0
Views: 1300
Reputation: 15375
AFAIK According to the documentation this must leak. Since Common Control ver. 6.0 you are repsonsible to delete the Bitmap. It is not enough to delete the Bitmap that was returned.
http://msdn.microsoft.com/en-us/library/windows/desktop/bb760782(v=vs.85).aspx
In version 6 of the Microsoft Win32 controls, a bitmap passed to a static control using the STM_SETIMAGE message was the same bitmap returned by a subsequent STM_SETIMAGE message. The client is responsible to delete any bitmap sent to a static control.
Upvotes: 0