Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit c29bf8f

Browse files
authored
[Windows] Fix GDI resource leaks in the software fallback path (#34574)
Updates `FlutterWindowWin32::OnBitmapSurfaceUpdated` to release the device context using `ReleaseDC`. Fixes: flutter/flutter#107368
1 parent 496c116 commit c29bf8f

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

shell/platform/windows/flutter_window_win32.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ void FlutterWindowWin32::OnResetImeComposing() {
249249
bool FlutterWindowWin32::OnBitmapSurfaceUpdated(const void* allocation,
250250
size_t row_bytes,
251251
size_t height) {
252-
HDC dc = ::GetDC(std::get<HWND>(GetRenderTarget()));
252+
HDC dc = ::GetDC(GetWindowHandle());
253253
BITMAPINFO bmi;
254254
memset(&bmi, 0, sizeof(bmi));
255255
bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
@@ -261,6 +261,7 @@ bool FlutterWindowWin32::OnBitmapSurfaceUpdated(const void* allocation,
261261
bmi.bmiHeader.biSizeImage = 0;
262262
int ret = SetDIBitsToDevice(dc, 0, 0, row_bytes / 4, height, 0, 0, 0, height,
263263
allocation, &bmi, DIB_RGB_COLORS);
264+
::ReleaseDC(GetWindowHandle(), dc);
264265
return ret != 0;
265266
}
266267

shell/platform/windows/flutter_window_win32_unittests.cc

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,20 @@ TEST(FlutterWindowWin32Test, CreateDestroy) {
211211
ASSERT_TRUE(TRUE);
212212
}
213213

214+
TEST(FlutterWindowWin32Test, OnBitmapSurfaceUpdated) {
215+
FlutterWindowWin32 win32window(100, 100);
216+
int old_handle_count = GetGuiResources(GetCurrentProcess(), GR_GDIOBJECTS);
217+
218+
constexpr size_t row_bytes = 100 * 4;
219+
constexpr size_t height = 100;
220+
std::array<char, row_bytes * height> allocation;
221+
win32window.OnBitmapSurfaceUpdated(allocation.data(), row_bytes, height);
222+
223+
int new_handle_count = GetGuiResources(GetCurrentProcess(), GR_GDIOBJECTS);
224+
// Check GDI resources leak
225+
EXPECT_EQ(old_handle_count, new_handle_count);
226+
}
227+
214228
// Tests that composing rect updates are transformed from Flutter logical
215229
// coordinates to device coordinates and passed to the text input manager
216230
// when the DPI scale is 100% (96 DPI).

0 commit comments

Comments
 (0)