Skip to content

Commit

Permalink
[Windows] Fix GDI resource leaks in the software fallback path
Browse files Browse the repository at this point in the history
  • Loading branch information
sevenstars committed Jul 10, 2022
1 parent 6c321db commit 16826a5
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
3 changes: 2 additions & 1 deletion shell/platform/windows/flutter_window_win32.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<HWND>(GetRenderTarget()));
HDC dc = ::GetDC(GetWindowHandle());
BITMAPINFO bmi;
memset(&bmi, 0, sizeof(bmi));
bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
Expand All @@ -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;
}

Expand Down
14 changes: 14 additions & 0 deletions shell/platform/windows/flutter_window_win32_unittests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<char, row_bytes * height> 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

0 comments on commit 16826a5

Please sign in to comment.