diff --git a/src/System.Windows.Forms.Primitives/tests/TestUtilities/IMallocSpy.cs b/src/System.Windows.Forms.Primitives/tests/TestUtilities/IMallocSpy.cs deleted file mode 100644 index 912ac995e20..00000000000 --- a/src/System.Windows.Forms.Primitives/tests/TestUtilities/IMallocSpy.cs +++ /dev/null @@ -1,71 +0,0 @@ -// 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. - -using System.Runtime.InteropServices; -using static Interop; - -namespace System -{ - [ComImport] - [Guid("0000001d-0000-0000-C000-000000000046")] - [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] - internal unsafe interface IMallocSpy - { - [PreserveSig] - nuint PreAlloc( - nuint cbRequest); - - [PreserveSig] - void* PostAlloc( - void* pActual); - - [PreserveSig] - void* PreFree( - void* pRequest, - BOOL fSpyed); - - [PreserveSig] - void PostFree( - BOOL fSpyed); - - [PreserveSig] - nuint PreRealloc( - void* pRequest, - nuint cbRequest, - void** ppNewRequest, - BOOL fSpyed); - - [PreserveSig] - void* PostRealloc( - void* pActual, - BOOL fSpyed); - - [PreserveSig] - void* PreGetSize( - void* pRequest, - BOOL fSpyed); - - [PreserveSig] - nuint PostGetSize( - nuint cbActual, - BOOL fSpyed); - - [PreserveSig] - void* PreDidAlloc( - void* pRequest, - BOOL fSpyed); - - [PreserveSig] - int PostDidAlloc( - void* pRequest, - BOOL fSpyed, - int fActual); - - [PreserveSig] - void PreHeapMinimize(); - - [PreserveSig] - void PostHeapMinimize(); - } -} diff --git a/src/System.Windows.Forms.Primitives/tests/TestUtilities/MallocSpy.cs b/src/System.Windows.Forms.Primitives/tests/TestUtilities/MallocSpy.cs deleted file mode 100644 index a314283b2a7..00000000000 --- a/src/System.Windows.Forms.Primitives/tests/TestUtilities/MallocSpy.cs +++ /dev/null @@ -1,35 +0,0 @@ -// 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. - -using static Interop; - -namespace System -{ - internal class MallocSpy : IMallocSpy - { - public virtual nuint PreAlloc(nuint cbRequest) => cbRequest; - public virtual unsafe void* PostAlloc(void* pActual) => pActual; - public virtual unsafe void* PreFree(void* pRequest, BOOL fSpyed) => pRequest; - public virtual void PostFree(BOOL fSpyed) { } - public virtual unsafe nuint PreRealloc(void* pRequest, nuint cbRequest, void** ppNewRequest, BOOL fSpyed) => cbRequest; - public virtual unsafe void* PostRealloc(void* pActual, BOOL fSpyed) => pActual; - public virtual unsafe void* PreGetSize(void* pRequest, BOOL fSpyed) => pRequest; - public virtual nuint PostGetSize(nuint cbActual, BOOL fSpyed) => cbActual; - public virtual unsafe void* PreDidAlloc(void* pRequest, BOOL fSpyed) => pRequest; - public virtual unsafe int PostDidAlloc(void* pRequest, BOOL fSpyed, int fActual) => fActual; - public virtual void PreHeapMinimize() { } - public virtual void PostHeapMinimize() { } - - internal class FreeTracker : MallocSpy - { - public List FreedBlocks { get; } = new(); - - public override unsafe void* PreFree(void* pRequest, BOOL fSpyed) - { - FreedBlocks.Add((IntPtr)pRequest); - return pRequest; - } - } - } -} diff --git a/src/System.Windows.Forms.Primitives/tests/TestUtilities/MallocSpyScope.MasterSpy.cs b/src/System.Windows.Forms.Primitives/tests/TestUtilities/MallocSpyScope.MasterSpy.cs deleted file mode 100644 index d58323f5093..00000000000 --- a/src/System.Windows.Forms.Primitives/tests/TestUtilities/MallocSpyScope.MasterSpy.cs +++ /dev/null @@ -1,99 +0,0 @@ -// 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. - -using static Interop; - -namespace System -{ - internal ref partial struct MallocSpyScope - { - /// - /// Redirecting spy that we register as a global. - /// - private class MasterSpy : IMallocSpy - { - private IMallocSpy _currentSpy; - private uint _registeredThread; - private readonly object _lock = new(); - - public IMallocSpy CurrentSpy - { - get - { - lock (_lock) - { - return (_registeredThread == 0 || _registeredThread == Kernel32.GetCurrentThreadId()) - ? _currentSpy : null; - } - } - } - - public void SetSpy(IMallocSpy spy, bool currentThreadOnly) - { - lock (_lock) - { - _currentSpy = spy; - _registeredThread = currentThreadOnly ? Kernel32.GetCurrentThreadId() : 0; - } - } - - public nuint PreAlloc(nuint cbRequest) => CurrentSpy?.PreAlloc(cbRequest) ?? cbRequest; - - public unsafe void* PostAlloc(void* pActual) - { - IMallocSpy current = CurrentSpy; - return current is null - ? pActual - : current.PostAlloc(pActual); - } - - public unsafe void* PreFree(void* pRequest, BOOL fSpyed) - { - IMallocSpy current = CurrentSpy; - return current is null - ? pRequest - : current.PreFree(pRequest, fSpyed); - } - - public void PostFree(BOOL fSpyed) => CurrentSpy?.PostFree(fSpyed); - - public unsafe nuint PreRealloc(void* pRequest, nuint cbRequest, void** ppNewRequest, BOOL fSpyed) - => CurrentSpy?.PreRealloc(pRequest, cbRequest, ppNewRequest, fSpyed) ?? cbRequest; - - public unsafe void* PostRealloc(void* pActual, BOOL fSpyed) - { - IMallocSpy current = CurrentSpy; - return current is null - ? pActual - : current.PostRealloc(pActual, fSpyed); - } - - public unsafe void* PreGetSize(void* pRequest, BOOL fSpyed) - { - IMallocSpy current = CurrentSpy; - return current is null - ? pRequest - : current.PreGetSize(pRequest, fSpyed); - } - - public nuint PostGetSize(nuint cbActual, BOOL fSpyed) - => CurrentSpy?.PostGetSize(cbActual, fSpyed) ?? cbActual; - - public unsafe void* PreDidAlloc(void* pRequest, BOOL fSpyed) - { - IMallocSpy current = CurrentSpy; - return current is null - ? pRequest - : current.PreDidAlloc(pRequest, fSpyed); - } - - public unsafe int PostDidAlloc(void* pRequest, BOOL fSpyed, int fActual) - => CurrentSpy?.PostDidAlloc(pRequest, fSpyed, fActual) ?? fActual; - - public void PreHeapMinimize() => CurrentSpy?.PreHeapMinimize(); - - public void PostHeapMinimize() => CurrentSpy?.PostHeapMinimize(); - } - } -} diff --git a/src/System.Windows.Forms.Primitives/tests/TestUtilities/MallocSpyScope.cs b/src/System.Windows.Forms.Primitives/tests/TestUtilities/MallocSpyScope.cs deleted file mode 100644 index 3e03c41cc48..00000000000 --- a/src/System.Windows.Forms.Primitives/tests/TestUtilities/MallocSpyScope.cs +++ /dev/null @@ -1,58 +0,0 @@ -// 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. - -using System.Runtime.InteropServices; -using static Interop; - -namespace System -{ - /// - /// Scope for registering and revoking a malloc spy class. - /// - internal ref partial struct MallocSpyScope - { - private static readonly object s_lock = new(); - private static readonly MasterSpy s_masterSpy = new(); - private static bool s_registered; - private readonly bool _lockTaken; - - public MallocSpyScope(IMallocSpy mallocSpy, bool currentThreadOnly = true) - { - _lockTaken = false; - Monitor.Enter(s_lock, ref _lockTaken); - - if (!s_registered) - { - // If another thread allocated while we were registered and hasn't freed everything yet, we can't - // deregister. As such we'll keep a permanent global spy and forward to whatever our current context is. - - var result = CoRegisterMallocSpy(s_masterSpy); - if (result.Failed()) - { - throw new InvalidOperationException(result.AsString()); - } - - s_registered = true; - } - - if (s_registered) - { - s_masterSpy.SetSpy(mallocSpy, currentThreadOnly); - } - } - - public void Dispose() - { - if (_lockTaken) - { - s_masterSpy.SetSpy(spy: null, currentThreadOnly: true); - - Monitor.Exit(s_lock); - } - } - - [DllImport("ole32.dll", ExactSpelling = true)] - private static extern HRESULT CoRegisterMallocSpy(IMallocSpy pMallocSpy); - } -} diff --git a/src/System.Windows.Forms.Primitives/tests/UnitTests/Interop/Ole32/CADWORDTests.cs b/src/System.Windows.Forms.Primitives/tests/UnitTests/Interop/Ole32/CADWORDTests.cs index 9cd405db8ce..21b3b06a838 100644 --- a/src/System.Windows.Forms.Primitives/tests/UnitTests/Interop/Ole32/CADWORDTests.cs +++ b/src/System.Windows.Forms.Primitives/tests/UnitTests/Interop/Ole32/CADWORDTests.cs @@ -10,26 +10,6 @@ namespace System.Windows.Forms.Primitives.Ole32Tests { public class CADWORDTests { - [Fact] - public void CADWORD_ConvertAndFree_FreesMemory() - { - List allocations = new(); - Ole32.CADWORD ca = CreateIntVector(allocations, 1970, 1999); - - MallocSpy.FreeTracker tracker = new(); - using MallocSpyScope scope = new(tracker); - - uint[] values = ca.ConvertAndFree(); - Assert.Equal(2, values.Length); - Assert.Equal(1970u, values[0]); - Assert.Equal(1999u, values[1]); - - foreach (IntPtr allocation in allocations) - { - Assert.Contains(allocation, tracker.FreedBlocks); - } - } - [Fact] public void CADWORD_ConvertAndFree_SingleItem() { diff --git a/src/System.Windows.Forms.Primitives/tests/UnitTests/Interop/Ole32/CALPOLESTRTests.cs b/src/System.Windows.Forms.Primitives/tests/UnitTests/Interop/Ole32/CALPOLESTRTests.cs index cf2bbac3656..5a018352be6 100644 --- a/src/System.Windows.Forms.Primitives/tests/UnitTests/Interop/Ole32/CALPOLESTRTests.cs +++ b/src/System.Windows.Forms.Primitives/tests/UnitTests/Interop/Ole32/CALPOLESTRTests.cs @@ -10,26 +10,6 @@ namespace System.Windows.Forms.Primitives.Ole32Tests { public class CALPOLESTRTests { - [Fact] - public void CALPOLESTR_ConvertAndFree_FreesMemory() - { - List allocations = new(); - Ole32.CALPOLESTR ca = CreateStringVector(allocations, "Sweet", "Potato"); - - MallocSpy.FreeTracker tracker = new(); - using MallocSpyScope scope = new(tracker); - - string?[] values = ca.ConvertAndFree(); - Assert.Equal(2, values.Length); - Assert.Equal("Sweet", values[0]); - Assert.Equal("Potato", values[1]); - - foreach (IntPtr allocation in allocations) - { - Assert.Contains(allocation, tracker.FreedBlocks); - } - } - [Fact] public void CALPOLESTR_ConvertAndFree_SingleItem() { diff --git a/src/System.Windows.Forms.Primitives/tests/UnitTests/Interop/Oleaut32/VARIANTTests.cs b/src/System.Windows.Forms.Primitives/tests/UnitTests/Interop/Oleaut32/VARIANTTests.cs index 475b3d34857..2f993cc61b9 100644 --- a/src/System.Windows.Forms.Primitives/tests/UnitTests/Interop/Oleaut32/VARIANTTests.cs +++ b/src/System.Windows.Forms.Primitives/tests/UnitTests/Interop/Oleaut32/VARIANTTests.cs @@ -6042,25 +6042,18 @@ public void ToObject_RECORDInvalidGuidNoData_ReturnsNull(Guid guid) GetGuidAction = () => (guid, HRESULT.S_OK) }; IntPtr pRecordInfo = Marshal.GetComInterfaceForObject(record); - try + using var variant = new VARIANT { - using var variant = new VARIANT + vt = VARENUM.RECORD, + data = new VARIANT.VARIANTUnion { - vt = VARENUM.RECORD, - data = new VARIANT.VARIANTUnion + recordVal = new VARIANT.VARIANTRecord { - recordVal = new VARIANT.VARIANTRecord - { - pRecInfo = pRecordInfo, - } + pRecInfo = pRecordInfo, } - }; - AssertToObjectEqual(null, variant); - } - finally - { - Marshal.Release(pRecordInfo); - } + } + }; + AssertToObjectEqual(null, variant); } [StaFact]