不是很明白资源泄漏的知识,大家帮找下那里存在资源泄漏,怎么释放?代码如下:

        CRect rcClient; 
GetClientRect(&rcClient); 
Bitmap bmp(rcClient.Width(), rcClient.Height());  Graphics * buffergraphics = Graphics::FromImage(&bmp); Image image1(L"outofsrv.bmp");
buffergraphics->DrawImage(&image1,0,0); Image image2(L"ClockBackchain.png");
int m_BakWidth  =image2.GetWidth();
int m_BakHeight =image2.GetHeight(); Point points[] = { Point(0, 0), 
               Point(m_BakWidth, 0), 
   Point(0, m_BakHeight)
};
buffergraphics->DrawImage(&image2,points,3); SolidBrush brush(Color(100,0,0,255));
    FontFamily fontFamily(L"Latha");
    Font font(&fontFamily,1,FontStyleRegular,UnitInch);
    PointF pointF(5,5);
    buffergraphics->DrawString(L"Hello Word!",-1,&font,pointF,&brush);

Graphics g(GetDC()->m_hDC); 
g.DrawImage(&bmp,0, 0, rcClient.Width(), rcClient.Height()); delete buffergraphics;

解决方案 »

  1.   

    CDC* dc = GetDC();
    Graphics g(dc->m_hDC); 
    g.DrawImage(&bmp,0, 0, rcClient.Width(), rcClient.Height());
    ReleaseDC(dc);
      

  2.   

    你怎么知道 Graphics::FromImage(&bmp) 是用 new 分配内存的?
    如果不清楚GDI+对象如何释放资源,建议使用CImage类,它是对GDI+的包装类。
      

  3.   

    楼主把代码改成这样试试:我这里改了以后没有内存增加了:
        CRect rcClient; GetClientRect(&rcClient); 
        Bitmap bmp( rcClient.Width(), rcClient.Height() ); 

        CDC* pdc = GetDC();
        Graphics g( pdc->m_hDC );    Graphics* buffergraphics = g.FromImage( &bmp );

        Image image1(L"1x.bmp");
        buffergraphics->DrawImage(&image1,0,0);

        Image image2(L"2x.bmp");
        int m_BakWidth  =image2.GetWidth();
        int m_BakHeight =image2.GetHeight();

        Point points[] = { Point(0, 0), Point(m_BakWidth, 0), Point(0, m_BakHeight ) };    buffergraphics->DrawImage(&image2,points,3);

        SolidBrush brush(Color(100,0,0,255));
        FontFamily fontFamily(L"Latha");
        Font font(&fontFamily,1,FontStyleRegular,UnitInch);
        PointF pointF(5,5);
        buffergraphics->DrawString(L"Hello Word!",-1,&font,pointF,&brush);    g.DrawImage( &bmp, 0, 0, rcClient.Width(), rcClient.Height());    delete buffergraphics;
        ReleaseDC(pdc);
      

  4.   

    查看任务管理器 GDI handle,也可能泄漏GetDC();需要ReleaseDC()释放,不然有GDI handle leak
      

  5.   

    http://www.codeproject.com/KB/GDI-plus/gdiplush.aspx使用此头文件能够检测到GDI+内存泄露
      

  6.   

    GDI+部分应该没有资源泄漏,GDI+本身提供的都是类,只要不用FromImage()之类的函数,一般不容易出现资源、内存泄露。
    另外,GetDC()返回的值没有用ReleaseDC释放。