我作了一个基于MS例子DrawCli的绘图程序,需要不断刷新某些图元,使用的是InvalidateRect做局部刷新,大概一个视图上有30个图元在以每秒两次的频率刷新。问题是每刷新一次都可以看到任务管理器里面程序占用的内存增长好几百K,越来越大,最多长到好几百M也不会停止。但是程序最小化后整个程序只占用2M,而且不会增长,一旦窗口可见又开始增长。程序关闭的时候也没有报告有显式的内存泄漏。请各位高手指点!!

解决方案 »

  1.   

    debug下调试看一下哪里的内存有问题咯,或网上找个查内存溢出的软件`
      

  2.   

    你的问题应该是GDI方面的泄漏,比如DC没有释放,或者HBITMAP没有释放,或者HPEN之类的GDI对象。
      

  3.   

    同意楼上,估计是dc资源没有释放,时间长了,button阿,菜单阿,显示都会变慢的
      

  4.   

    OnDraw里面是这样写的,那请问高手有什么改进建议啊
    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);
    }
      

  5.   

    pDrawDC->Release;
    deleteobject(pOldBitmap);
      

  6.   

    确实是程序一直在申请新资源,一直到程序结束才能释放所有申请的资源。所有整体上看没有资源泄漏,但是程序使用的内存在不断疯狂增长。最终诊断为BSTR没有调用SysFreeString进行释放。感谢各位参与!