程序如下执行以下的程序后程序的速度渐渐变慢可用内存数量越来越少
void CSmallView::OnMouseMove(UINT nFlags, CPoint point) 
{
// TODO: Add your message handler code here and/or call default
CRect tt;
 CDC *pDC1=this->GetDC();
GetClientRect(tt);
tt.bottom=tt.bottom/0.3;
tt.right=tt.right/0.3;
pDC1->SetMapMode(MM_ANISOTROPIC);
pDC1->SetViewportExt(pDC1->GetDeviceCaps(LOGPIXELSX)*0.4,
pDC1->GetDeviceCaps(LOGPIXELSY)*0.4);
pDC1->SetWindowExt(100, 100);

pDC1->DPtoLP(&point,1);

if(pressflag){
CSize offset(point-firstpoint);
four[0]=four[0]+offset;
//four[1]=four[1]+offset;
//four[2]=four[2]+offset;
//four[3]=four[3]+offset;
firstpoint=point;
OnPaint1(doc,view);
view->refresh(four[0].x,four[0].y); }
CDialog::OnMouseMove(nFlags, point);
}
后来发现是CDC *pDC=this->GetDC()的原因这是怎么回事怎么解决?

解决方案 »

  1.   

    GetDC() 后一定要调用 ReleaseDC() 才行, 这是DC 这种东东的特性
      

  2.   

    在你的代碼后加
    this->ReleaseDC(pDC)
      

  3.   

    For common device contexts, GetDC assigns default attributes to the context each time it is retrieved. For class and private contexts, GetDC leaves the previously assigned attributes unchanged. The device context can be used in subsequent graphics device interface (GDI) functions to draw in the client area.Unless the device context belongs to a window class, the CWnd::ReleaseDC method must be called to release the context after painting. Since only five common device contexts are available at any given time, failure to release a device context can prevent other applications from accessing a device context. 
      

  4.   

    统一楼上的
    GetDC()和ReleaseDC必须配对的
    把GetDC提到循环外面,循环结束后再ReleaseDC
      

  5.   

    CDC *pDC1=this->GetDC();并不是内存泄漏。就像CString的GetBuffer函数一样,好像会分配内存,但并不是动态内存,所以不是内存泄漏。但却占用内存。因此每次用完后要ReleaseDC(pDC)。将这块内存释放。
      

  6.   

    那不是内存泄漏,这是标准的资源泄漏,如果你频繁使用CPen,CBrush等,而不使得dc复员,也会出现资源泄漏。避免的方法:GetDC与ReleaseDC配合使用,oldpen = dc.SelectObject(newpen)和dc.SelectObject(oldpen)配合使用等等
      

  7.   

    呵呵,来晚了,我前两天测试的时候也出现这样的问题,gdi对象数不断的增加,和设备环境有关的一定要小心