Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Release/5.0] Fix GDI handle leak in Graphics.DrawIcon #48189

Merged
merged 3 commits into from
Feb 12, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/libraries/Common/tests/System/Drawing/Helpers.cs
Original file line number Diff line number Diff line change
@@ -182,6 +182,9 @@ private static Rectangle GetRectangle(RECT rect)
[DllImport("user32.dll", SetLastError = true)]
public static extern IntPtr GetForegroundWindow();

[DllImport("user32.dll", ExactSpelling = true, SetLastError = true)]
public static extern int GetGuiResources(IntPtr hProcess, uint flags);

[DllImport("user32.dll", SetLastError = true)]
public static extern IntPtr GetDC(IntPtr hWnd);

4 changes: 2 additions & 2 deletions src/libraries/System.Drawing.Common/Directory.Build.props
Original file line number Diff line number Diff line change
@@ -4,7 +4,7 @@
<StrongNameKeyId>Open</StrongNameKeyId>
<IncludePlatformAttributes>true</IncludePlatformAttributes>
<UnsupportedOSPlatforms>browser</UnsupportedOSPlatforms>
<PackageVersion>5.0.1</PackageVersion>
<AssemblyVersion>5.0.0.1</AssemblyVersion>
<PackageVersion>5.0.2</PackageVersion>
<AssemblyVersion>5.0.0.2</AssemblyVersion>
</PropertyGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -379,7 +379,9 @@ private void DrawIcon(IntPtr dc, Rectangle imageRect, Rectangle targetRect, bool
}
finally
{
RestoreClipRgn(dc, hSaveRgn);
Interop.Gdi32.SelectClipRgn(dc, hSaveRgn);
// We need to delete the region handle after restoring the region as GDI+ uses a copy of the handle.
Interop.Gdi32.DeleteObject(hSaveRgn);
}
}

@@ -394,15 +396,15 @@ private static IntPtr SaveClipRgn(IntPtr hDC)
hSaveRgn = hTempRgn;
hTempRgn = IntPtr.Zero;
}
else
{
// if we fail to get the clip region delete the handle.
Interop.Gdi32.DeleteObject(hTempRgn);
}

return hSaveRgn;
}

private static void RestoreClipRgn(IntPtr hDC, IntPtr hRgn)
{
Interop.Gdi32.SelectClipRgn(new HandleRef(null, hDC), new HandleRef(null, hRgn));
}

internal void Draw(Graphics graphics, int x, int y)
{
Size size = Size;
59 changes: 59 additions & 0 deletions src/libraries/System.Drawing.Common/tests/GdiPlusHandlesTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Diagnostics;
using System.Runtime.InteropServices;
using Microsoft.DotNet.RemoteExecutor;
using Xunit;
using Xunit.Sdk;

namespace System.Drawing.Tests
{
[PlatformSpecific(TestPlatforms.Windows)]
public static class GdiPlusHandlesTests
{
public static bool IsDrawingAndRemoteExecutorSupported => Helpers.GetIsDrawingSupported() && RemoteExecutor.IsSupported;

[ConditionalFact(nameof(IsDrawingAndRemoteExecutorSupported))]
public static void GraphicsDrawIconDoesNotLeakHandles()
{
RemoteExecutor.Invoke(() =>
{
const int handleTreshold = 1;
using Bitmap bmp = new(100, 100);
using Icon ico = new(Helpers.GetTestBitmapPath("16x16_one_entry_4bit.ico"));

IntPtr hdc = Helpers.GetDC(Helpers.GetForegroundWindow());
using Graphics graphicsFromHdc = Graphics.FromHdc(hdc);

using Process currentProcess = Process.GetCurrentProcess();
IntPtr processHandle = currentProcess.Handle;

int initialHandles = Helpers.GetGuiResources(processHandle, 0);
ValidateNoWin32Error(initialHandles);

for (int i = 0; i < 5000; i++)
{
graphicsFromHdc.DrawIcon(ico, 100, 100);
}

int finalHandles = Helpers.GetGuiResources(processHandle, 0);
ValidateNoWin32Error(finalHandles);

Assert.InRange(finalHandles, initialHandles, initialHandles + handleTreshold);
}).Dispose();
}

private static void ValidateNoWin32Error(int handleCount)
{
if (handleCount == 0)
{
int error = Marshal.GetLastWin32Error();

if (error != 0)
throw new XunitException($"GetGuiResorces failed with win32 error: {error}");
}
}

}
}
Original file line number Diff line number Diff line change
@@ -22,6 +22,7 @@
<Compile Include="DrawingTest.cs" />
<Compile Include="FontTests.cs" />
<Compile Include="FontFamilyTests.cs" />
<Compile Include="GdiPlusHandlesTests.cs" />
<Compile Include="GdiplusTests.cs" />
<Compile Include="GraphicsTests.cs" />
<Compile Include="Graphics_DrawBezierTests.cs" />
1 change: 1 addition & 0 deletions src/libraries/libraries-packages.proj
Original file line number Diff line number Diff line change
@@ -21,6 +21,7 @@
<ProjectReference Include="$(PkgDir)runtime.native.System.IO.Ports\runtime.native.System.IO.Ports.proj" />
<ProjectReference Include="$(LibrariesProjectRoot)\System.IO.Ports\pkg\System.IO.Ports.pkgproj" Condition="'$(BuildAllConfigurations)' == 'true'" />
<!-- This is merge marker 1 to help automerge -->
<ProjectReference Include="$(LibrariesProjectRoot)\System.Drawing.Common\pkg\System.Drawing.Common.pkgproj" Condition="'$(BuildAllConfigurations)' == 'true'" />
<!-- This is merge marker 2 to help automerge -->
<!-- This is merge marker 3 to help automerge -->
<!-- This is merge marker 4 to help automerge -->
6 changes: 4 additions & 2 deletions src/libraries/pkg/baseline/packageIndex.json
Original file line number Diff line number Diff line change
@@ -2904,7 +2904,8 @@
"4.7.1",
"4.7.2",
"5.0.0",
"5.0.1"
"5.0.1",
"5.0.2"
],
"BaselineVersion": "5.0.0",
"InboxOn": {
@@ -2925,7 +2926,8 @@
"4.0.2.1": "4.7.1",
"4.0.2.2": "4.7.2",
"5.0.0.0": "5.0.0",
"5.0.0.1": "5.0.1"
"5.0.0.1": "5.0.1",
"5.0.0.2": "5.0.2"
}
},
"System.Drawing.Design": {