Track disposal of NativeImageList

This commit is contained in:
Igor Velikorossov 2020-06-29 16:59:12 +10:00
Родитель dfd8105aa6
Коммит 33ac99ea6f
1 изменённых файлов: 19 добавлений и 10 удалений

Просмотреть файл

@ -1,9 +1,8 @@
// Licensed to the .NET Foundation under one or more agreements.
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
#nullable disable
using System.Diagnostics;
using static Interop;
namespace System.Windows.Forms
@ -12,20 +11,19 @@ namespace System.Windows.Forms
{
internal class NativeImageList : IDisposable, IHandle
{
private IntPtr _himl;
#if DEBUG
private readonly string _callStack;
#endif
internal NativeImageList(IntPtr himl)
{
_himl = himl;
Handle = himl;
#if DEBUG
_callStack = Environment.StackTrace;
#endif
}
public IntPtr Handle => _himl;
public IntPtr Handle { get; private set; }
public void Dispose()
{
@ -35,14 +33,25 @@ namespace System.Windows.Forms
public void Dispose(bool disposing)
{
if (_himl != IntPtr.Zero)
if (Handle != IntPtr.Zero)
{
ComCtl32.ImageList.Destroy(_himl);
_himl = IntPtr.Zero;
ComCtl32.ImageList.Destroy(Handle);
Handle = IntPtr.Zero;
}
#if DEBUG
GC.SuppressFinalize(this);
#endif
}
~NativeImageList() => Dispose(false);
~NativeImageList()
{
#if DEBUG
Debug.Fail($"{nameof(NativeImageList)} was not disposed properly. Originating stack:\n{_callStack}");
#endif
Dispose(false);
}
}
}
}