我作了一个基于MS例子DrawCli的绘图程序,需要不断刷新某些图元,使用的是InvalidateRect做局部刷新,大概一个视图上有30个图元在以每秒两次的频率刷新。问题是每刷新一次都可以看到任务管理器里面程序占用的内存增长好几百K,越来越大,最多长到好几百M也不会停止。但是程序最小化后整个程序只占用2M,而且不会增长,一旦窗口可见又开始增长。程序关闭的时候也没有报告有显式的内存泄漏。请各位高手指点!!
调试欢乐多
CGraphicYardEditorDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
CDC dc;
CDC* pDrawDC = pDC;
CBitmap bitmap;
CBitmap* pOldBitmap;
// only paint the rect that needs repainting
CRect client;
pDC->GetClipBox(client);
CRect rect = client;
DocToClient(rect);
if (!pDC->IsPrinting())
{
// draw to offscreen bitmap for fast looking repaints
if (dc.CreateCompatibleDC(pDC))
{
if (bitmap.CreateCompatibleBitmap(pDC, rect.Width(), rect.Height()))
{
OnPrepareDC(&dc, NULL);
pDrawDC = &dc;
// offset origin more because bitmap is just piece of the whole drawing
dc.OffsetViewportOrg(-rect.left, -rect.top);
pOldBitmap = dc.SelectObject(&bitmap);
dc.SetBrushOrg(rect.left % 8, rect.top % 8);
// might as well clip to the same rectangle
dc.IntersectClipRect(client);
}
} // paint background
CBrush brush;
COLORREF clr = GetGYard()->GetBkColor().GetValue() & 0xffffff;
if (!brush.CreateSolidBrush(clr))
return;
brush.UnrealizeObject();
pDrawDC->FillRect(client, &brush);
}
Graphics *m_pGraphics = Graphics::FromHDC(pDrawDC->m_hDC);
GetGYard()->Draw(m_pGraphics, pDrawDC->IsPrinting());#ifdef __GYARD_EDITOR__
if(!pDC->IsPrinting())
{//选中和高亮提示只在屏幕上显示时出现
POSITION pos = m_selection.GetHeadPosition();
while(pos)
m_selection.GetNext(pos)->DrawTracker(m_pGraphics, CDrawElement::selected); pos = m_lstHighlight.GetHeadPosition();
while(pos)
m_lstHighlight.GetNext(pos)->DrawHighLight(m_pGraphics, m_ptCurrPt, m_highLight);
}
#endif
if (pDrawDC != pDC)
{
pDC->SetViewportOrg(0, 0);
pDC->SetWindowOrg(0,0);
pDC->SetMapMode(MM_TEXT);
dc.SetViewportOrg(0, 0);
dc.SetWindowOrg(0,0);
dc.SetMapMode(MM_TEXT);
pDC->BitBlt(rect.left, rect.top, rect.Width(), rect.Height(),
&dc, 0, 0, SRCCOPY);
dc.SelectObject(pOldBitmap);
}
deleteobject(pOldBitmap);