Home > Software engineering >  MFC loading picture memory leak problem
MFC loading picture memory leak problem

Time:10-08

MFC loading picture memory leaks, but couldn't find the reason, bother everybody to help see,
The code is as follows:
Void CLoadBmpDlg: : DispBmpByPath ()
{
CBitmap HBMP;
HBITMAP HBITMAP;
CStatic * pStaic;

PStaic=(CStatic *) GetDlgItem (IDC_ID_PHOTO);//control ID
Hbitmap=SHLoadDIBitmap (_T (" \ \ LoadBmp \ \ Id BMP "));//to load the BMP path

If (NULL==hbitmap)
return;
HBMP. Attach (hbitmap);
BITMAP bm.
HBMP. GetBitmap (& amp; Bm);
The CDC dcMem.
DcMem. CreateCompatibleDC (GetDC ());
CBitmap * poldBitmap=(CBitmap *) dcMem. SelectObject (HBMP);
CRect lRect;
PStaic - & gt; GetClientRect (& amp; LRect);
PStaic - & gt; GetDC () - & gt; StretchBlt (lRect lRect. Left, top, lRect. The Width (), lRect. Height (),
& DcMem, 0, 0, bm bmWidth, bm. BmHeight, WHITENESS);
PStaic - & gt; GetDC () - & gt; StretchBlt (lRect lRect. Left, top, lRect. The Width (), lRect. Height (),
& DcMem, 0, 0, bm bmWidth, bm. BmHeight, SRCCOPY);
DcMem. SelectObject (& amp; PoldBitmap);
DcMem. DeleteDC ();
HBMP. DeleteObject ();
}

CodePudding user response:

"For each call to GetDC, you must have a subsequent call to ReleaseDC."
So dcMem. CreateCompatibleDC (GetDC ()); I need to change the code, save the GetDC () to get the DC, then the ReleaseDC (),

CodePudding user response:

The CDC * pDC=GetDC ();//a!
Other use pDC, not many GetDC; (system didn't assign you a temporary DC)
The ReleaseDC ()
  • Related