请教:vc载入BMP图片时资源泄露
载入的bmp文件2.5M,每次运行这段代码都会有5M左右的内存泄露
我不太熟悉如何释放资源,后边的释放资源部分可能不对,反复试验都不成。不晓得如何释放,高手请给看一下,困扰我两天多了,谢谢
void CprintPreview::OnPaint()
{
CPaintDC dc(this); // device context for painting
CDC *hdcprint=&dc;
CDC memDC;
CBitmap bitmap;
BITMAP bm;
HBITMAP hbitmap=(HBITMAP)LoadImage(0,"C:\\PIA\\datacurveReport.bmp",IMAGE_BITMAP,0,0,
LR_CREATEDIBSECTION|LR_DEFAULTSIZE|LR_LOADFROMFILE);
GetObject(hbitmap, sizeof(BITMAP), (LPBYTE)&bm);
int bmpWidth =bm.bmWidth;
int bmpHeight=bm.bmHeight;
memDC.CreateCompatibleDC(hdcprint);
bitmap.CreateCompatibleBitmap(hdcprint,bmpWidth,bmpHeight);
bitmap.Attach(hbitmap);
bitmap.GetObject(sizeof(bm),&bm);
CBitmap *pOldBitmap = (CBitmap *)memDC.SelectObject(&bitmap);
if(pOldBitmap == NULL)
{
memDC.DeleteDC();
DeleteObject( &bitmap );
bitmap.DeleteObject();
AfxMessageBox(_T("Not enough resource."));
return;
}
hdcprint->StretchBlt(0, 0, bmpWidth, bmpHeight, &memDC,0, 0, bmpWidth,bmpHeight, SRCCOPY);
memDC.SelectObject(pOldBitmap);
bitmap.Detach();
DeleteObject( hbitmap );
bitmap.DeleteObject();
DeleteObject( &bitmap );
if(!memDC.DeleteDC() )
AfxMessageBox("fail delete memDC");
hdcprint->DeleteDC();
ReleaseDC(hdcprint);
}