Monday, 26 August 2013

How to correct the GDI resource leakage?

How to correct the GDI resource leakage?

I found out that after FillRgn() Windows GDI API function, the GDI object
used in this function is somehow "stuck" somewhere in the internal system
maps and won't delete properly: calling DeleteObject() on the object
returns successfully, but the number of GDI objects for the process does
not decrement. Here is the code:
void gditest()
{
HBRUSH h = CreateSolidBrush(RGB(255, 237, 5));
HRGN rgn = CreateRectRgn(0, 100, 100, 0);
FillRgn(g_DC, rgn, h);
int before = GetGuiResources(GetCurrentProcess(), GR_GDIOBJECTS);
SelectObject(g_DC, GetStockObject(WHITE_BRUSH));
int rs = DeleteObject( h );
if ( !rs )
throw;
int after = GetGuiResources(GetCurrentProcess(), GR_GDIOBJECTS);
}
The code demonstrates that after deleting the HBRUSH handle variables
'before' and 'after' are equal; g_DC is the main window HDC.
How to delete the 'h' so that the number of GDI objects were decrementing?

No comments:

Post a Comment