3

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?

6
  • You are leaking the region handle. Is that perhaps the leak you are observing? What you do with h looks OK to me; in fact, SelectObject call is redundant since you've never actually selected your brush into g_DC. Aug 26, 2013 at 23:08
  • I forgot to mention that the resource in consideration is HBRUSH. Yes, the region is not deleted, but the point of the test code is that after deleting HBRUSH the number of GDI objects doesn't decrement, as I suppose it should.
    – Al Berger
    Aug 26, 2013 at 23:19
  • 3
    As noted in this article, GDI caches solid color brushes. What you're seeing is that the brush was logically deleted, but is still physically present in the cache. Aug 26, 2013 at 23:42
  • @Ray - yes, it seems like an answer, just as I supposed that the object is "stuck" in the OS. Strangely that there is not a word about this caching in the 'official' API documentation since this looks like leakage.
    – Al Berger
    Aug 27, 2013 at 0:02
  • 1
    The behavior is not documented because it is not contractual. It is an implementation detail. (Some versions of Windows do not have a solid color brush cache.) Aug 27, 2013 at 1:27

2 Answers 2

1

When calling SelectObject() for the first time, you have to remember the return value and select it again once you are done with the DC. Also, you have to delete all created GDI objects.

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);
    HBRUSH oldBrush = SelectObject(g_DC, GetStockObject(WHITE_BRUSH));
    SelectObject( g_DC, oldBrush );
    int rs = DeleteObject( h );
    if ( !rs )
        throw;
    DeleteObject( rgn );
    int after = GetGuiResources(GetCurrentProcess(), GR_GDIOBJECTS);
}

Note:
Objects retrieved by GetStockObject() can be deleted, but they don't have to.

2
  • As Igor mentioned above, the SelectObject() call is redundant and placed here just for ensuring that the HBRUSH 'h' is not selected in the g_DC, if it was selected somewhere in the internals of FillRgn().
    – Al Berger
    Aug 26, 2013 at 23:30
  • I put it there just to emphasize my point. Anyway, I would suggest writing RAII wrappers for GDI objects. This way you will shoot yourself in a foot, sooner or later. (obviously you already did) Aug 26, 2013 at 23:32
1

Either GDI is caching the brush and region resources, or it's a bug. The count does not go down after deleting the brush or the region. Tested on Windows 7. Here's my quick-and-dirty repro code:

#include <cassert>
#include <iostream>
#include <windows.h>

void PrintGdiCount() {
  std::cout << ::GetGuiResources(::GetCurrentProcess(), GR_GDIOBJECTS)
            << std::endl;
}

int main() {
  PrintGdiCount();
  ::GdiSetBatchLimit(1);  // disable batching
  HDC hdcScreen = ::GetDC(NULL);
  PrintGdiCount();
  HDC hdcMemory = ::CreateCompatibleDC(hdcScreen);
  PrintGdiCount();
  HBITMAP hbmpMemory = ::CreateCompatibleBitmap(hdcScreen, 100, 100);
  PrintGdiCount();
  HBITMAP hbmpOld = reinterpret_cast<HBITMAP>(::SelectObject(hdcMemory, hbmpMemory));
  PrintGdiCount();
  HBRUSH hbrush = ::CreateSolidBrush(RGB(255, 127, 32));
  PrintGdiCount();
  HRGN hrgn = ::CreateRectRgn(0, 0, 50, 50);
  PrintGdiCount();
//  ::FillRgn(hdcMemory, hrgn, hbrush);  // doesn't affect GDI count
  PrintGdiCount();
  BOOL bDeletedBrush = ::DeleteObject(hbrush);
  assert(bDeletedBrush);
  PrintGdiCount();  // expected decrement, but it doesn't
  ::DeleteObject(hrgn);
  PrintGdiCount();  // expected decrement, but it doesn't
  ::SelectObject(hdcMemory, hbmpOld);
  ::DeleteObject(hbmpMemory);
  PrintGdiCount();
  ::DeleteDC(hdcMemory);
  PrintGdiCount();
  ::ReleaseDC(NULL, hdcScreen);
  PrintGdiCount();  // result is 2 higher than initial count
  return 0;
}
2
  • Try calling GdiFlush() and see if it makes a difference. If the OS is batching the fills then it must by necessity be caching the brush. Aug 27, 2013 at 0:10
  • @Jonathan Potter: I disabled batching with GdiSetBatchLimit(1) at the beginning, so GdiFlush() should be irrelevant. Aug 27, 2013 at 0:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.