memory leak due to BUG:GETDC () ReleaseDC ( )Today colleagues in a timer to draw, the results found that memory is slow growth, suspected memory leaks, check for a long time, the release of GDI resources are released Ah, but still cannot solve, finally put GetDC () in the initialization, the exit call ReleaseDC (), Intermediate processing part of the direct use of the PDC, memory is not growing, but always feel strange, and carefully check the next, the original this is a bug in MFC. Each call has a 4-byte memory leak, specifically the following link: http://support.microsoft.com/kb/ 819635/zh-cn
Steps to reproduce the BehaviorIn a method, which is a part of a class this is the derived from
CWnd, insert the following code in your application:
CDC *PDC;
Rect rect;
GetClientRect (&rect);
for (int i = 0; i < i++)
{
PDC = GetDC ();
ReleaseDC (PDC);
}
If you run this code and then check the system memory before and after your run the code, you notice the system memory Leaks four bytes per iteration. If you change the code to the following code, the memory leak does not occur:
HDC hdc;
Rect rect;
:: GetClientRect (M_hwnd, &rect);
for (int i = 0; i < i++)
{
HDC =:: GetDC (M_hwnd);
::D rawtext (HDC, L "Testing ...", &rect, Dt_ CENTER);
:: ReleaseDC (M_hwnd, HDC);
}