请教: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);
}
...全文
148 5 点赞 打赏 收藏 举报
写回复
5 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
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);

  • 打赏
  • 举报
回复
相关推荐
发帖
图形处理/算法
创建于2007-09-28

1.9w+

社区成员

VC/MFC 图形处理/算法
申请成为版主
帖子事件
创建了帖子
2009-08-28 10:07
社区公告
暂无公告