GDI 正在缓存画笔和区域资源,或者它是一个错误。删除画笔或区域后计数不会下降。在 Windows 7 上测试。这是我的快速和肮脏的重现代码:
#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;
}