본문 바로가기
Programming/MFC

ReleaseDC 위치가 중요한가?

by deviAk 2008. 6. 20.
반응형

스프라이트 툴 제작 중 ZoomDlg 에서 이상하게 메모리 누수가 발생 하였다..

대충 소스는 다음과 같이 작성을 하였었다..

{
CClientDC dc(this);
CDC* pDC = CWnd::GetDC();

...                    // dc를 얻어와 Dialog 창에 뿌리는 작업 들..
...                    
...                    
...                           
...                    
...                    
...                    
...                    
dc.StretchBlt(0, 0, drt.right, drt.bottom, pDC, spt.x, spt.y, sWidth, sHeight, SRCCOPY);

dc.MoveTo(drt.right/2, drt.top);
dc.LineTo(drt.right/2, drt.bottom);

dc.MoveTo(drt.left, drt.bottom/2);
dc.LineTo(drt.right, drt.bottom/2);

ReleaseDC(pDC);
}

이상하게 위의 소스에서는 계속적인 메모리 누수가 있었다..

그러다가 혹시나 해서 GetDC의 위치와 ReleaseDC의 위치를 바꾸어 보니 메모리 누수가 싹 사라졌다.

{
CClientDC dc(this);

...                    // dc를 얻어와 Dialog 창에 뿌리는 작업 들..
...                    
...                    
...                           
...                    
...                    
...                    
CDC* pDC = CWnd::GetDC();
dc.StretchBlt(0, 0, drt.right, drt.bottom, pDC, spt.x, spt.y, sWidth, sHeight, SRCCOPY);
ReleaseDC(pDC);

dc.MoveTo(drt.right/2, drt.top);
dc.LineTo(drt.right/2, drt.bottom);

dc.MoveTo(drt.left, drt.bottom/2);
dc.LineTo(drt.right, drt.bottom/2);
}


아직은 왜 그런지 모르겠지만 앞으로 위 방식처럼 코딩을 하던 원인을 찾아봐야 겠다.
반응형