-
Notifications
You must be signed in to change notification settings - Fork 4.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix GDI handle leak in Icon.DrawImage (#47836)
* Fix GDI handle leak in Icon.DrawImage * Fix build * Add test * Fix test in Mono * Add missing Usings
- Loading branch information
Showing
5 changed files
with
71 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
src/libraries/System.Drawing.Common/tests/GdiPlusHandlesTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}"); | ||
} | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters