From 16826a5377185a0cd3eb072ac57f8d61523a8977 Mon Sep 17 00:00:00 2001 From: Qixing Cao Date: Sun, 10 Jul 2022 19:23:55 +0800 Subject: [PATCH] [Windows] Fix GDI resource leaks in the software fallback path --- shell/platform/windows/flutter_window_win32.cc | 3 ++- .../windows/flutter_window_win32_unittests.cc | 14 ++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/shell/platform/windows/flutter_window_win32.cc b/shell/platform/windows/flutter_window_win32.cc index 1815a6c0ddef9..2c8f47f172f84 100644 --- a/shell/platform/windows/flutter_window_win32.cc +++ b/shell/platform/windows/flutter_window_win32.cc @@ -247,7 +247,7 @@ void FlutterWindowWin32::OnResetImeComposing() { bool FlutterWindowWin32::OnBitmapSurfaceUpdated(const void* allocation, size_t row_bytes, size_t height) { - HDC dc = ::GetDC(std::get(GetRenderTarget())); + HDC dc = ::GetDC(GetWindowHandle()); BITMAPINFO bmi; memset(&bmi, 0, sizeof(bmi)); bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER); @@ -259,6 +259,7 @@ bool FlutterWindowWin32::OnBitmapSurfaceUpdated(const void* allocation, bmi.bmiHeader.biSizeImage = 0; int ret = SetDIBitsToDevice(dc, 0, 0, row_bytes / 4, height, 0, 0, 0, height, allocation, &bmi, DIB_RGB_COLORS); + ::ReleaseDC(GetWindowHandle(), dc); return ret != 0; } diff --git a/shell/platform/windows/flutter_window_win32_unittests.cc b/shell/platform/windows/flutter_window_win32_unittests.cc index 063f51a77e96d..e96ed0b600804 100644 --- a/shell/platform/windows/flutter_window_win32_unittests.cc +++ b/shell/platform/windows/flutter_window_win32_unittests.cc @@ -327,5 +327,19 @@ TEST(FlutterWindowWin32Test, OnPointerStarSendsDeviceType) { kDefaultPointerDeviceId); } +TEST(FlutterWindowWin32Test, OnBitmapSurfaceUpdated) { + FlutterWindowWin32 win32window(100, 100); + int old_handle_count = GetGuiResources(GetCurrentProcess(), GR_GDIOBJECTS); + + constexpr size_t row_bytes = 100 * 4; + constexpr size_t height = 100; + std::array allocation; + win32window.OnBitmapSurfaceUpdated(allocation.data(), row_bytes, height); + + int new_handle_count = GetGuiResources(GetCurrentProcess(), GR_GDIOBJECTS); + // Check GDI resources leak + EXPECT_EQ(old_handle_count, new_handle_count); +} + } // namespace testing } // namespace flutter