请教:vc载入BMP图片时资源泄露

chencunzhu 2009-08-28 10:07:31
载入的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);
}
...全文
176 5 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
aa3000 2009-08-28
  • 打赏
  • 举报
回复
写错了...

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.Attach(hbitmap);
CBitmap *pOldBitmap = (CBitmap *)memDC.SelectObject(&bitmap);
hdcprint->StretchBlt(0, 0, bmpWidth, bmpHeight, &memDC,0, 0, bmpWidth,bmpHeight, SRCCOPY);

memDC.SelectObject(pOldBitmap);

bitmap.DeleteObject();

if(!memDC.DeleteDC() )
AfxMessageBox("fail delete memDC");
rambo_ghaip 2009-08-28
  • 打赏
  • 举报
回复
开始,你用hbitmap指向了磁盘上的图片.
HBITMAP hbitmap=(HBITMAP)LoadImage(0,"C:\\PIA\\datacurveReport.bmp",IMAGE_BITMAP,0,0,
LR_CREATEDIBSECTION|LR_DEFAULTSIZE|LR_LOADFROMFILE);

后来,你又用hbitmap指向了你创建的图
bitmap.CreateCompatibleBitmap(hdcprint,bmpWidth,bmpHeight);
bitmap.Attach(hbitmap);

所以原先磁盘上加载的图片占用的资源就没有释放
aa3000 2009-08-28
  • 打赏
  • 举报
回复
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.Attach(hbitmap);
CBitmap *pOldBitmap = (CBitmap *)memDC.SelectObject(&bitmap);
if(pOldBitmap == NULL)
{
memDC.DeleteDC();
bitmap.DeleteObject();
AfxMessageBox(_T("Not enough resource."));
return;
}
hdcprint->StretchBlt(0, 0, bmpWidth, bmpHeight, &memDC,0, 0, bmpWidth,bmpHeight, SRCCOPY);

memDC.SelectObject(pOldBitmap);

bitmap.DeleteObject();

if(!memDC.DeleteDC() )
AfxMessageBox("fail delete memDC");

youyifang 2009-08-28
  • 打赏
  • 举报
回复
pOldBitmap 资源释放看看
dirdirdir3 2009-08-28
  • 打赏
  • 举报
回复
GetObject(hbitmap, sizeof(BITMAP), (LPBYTE)&bm);
int bmpWidth =bm.bmWidth;
int bmpHeight=bm.bmHeight;

memDC.CreateCompatibleDC(hdcprint);
bitmap.Attach(hbitmap);
bitmap.GetObject(sizeof(bm),&bm);
CBitmap *pOldBitmap = (CBitmap *)memDC.SelectObject(&bitmap);
hdcprint->StretchBlt(0, 0, bmpWidth, bmpHeight, &memDC,0, 0, bmpWidth,bmpHeight, SRCCOPY);

19,472

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 图形处理/算法
社区管理员
  • 图形处理/算法社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧