Skip to content

Commit

Permalink
Revert "Added caching of event registration state to support unsubscr…
Browse files Browse the repository at this point in the history
…ibes across garbage collections (#861)"

This reverts commit fa038af.
  • Loading branch information
Scottj1s committed Jun 15, 2021
1 parent 0bc7f16 commit 0f079e6
Show file tree
Hide file tree
Showing 12 changed files with 107 additions and 322 deletions.
43 changes: 0 additions & 43 deletions src/Tests/TestComponentCSharp/Singleton.cpp

This file was deleted.

19 changes: 0 additions & 19 deletions src/Tests/TestComponentCSharp/Singleton.h

This file was deleted.

13 changes: 1 addition & 12 deletions src/Tests/TestComponentCSharp/TestComponentCSharp.idl
Original file line number Diff line number Diff line change
Expand Up @@ -130,17 +130,6 @@ namespace TestComponentCSharp
static Int32 NumObjects{ get; };
}

interface ISingleton
{
Int32 IntProperty;
event Windows.Foundation.EventHandler<Int32> IntPropertyChanged;
}

static runtimeclass Singleton
{
static ISingleton Instance;
}

[default_interface, gc_pressure(Windows.Foundation.Metadata.GCPressureAmount.High)]
runtimeclass Class :
Windows.Foundation.IStringable
Expand Down Expand Up @@ -464,7 +453,7 @@ namespace TestComponentCSharp
static event Windows.Foundation.EventHandler<Int32> WarningEvent;
}
}

[contract(Windows.Foundation.UniversalApiContract, 8)]
interface IWarning1
{
Expand Down
2 changes: 0 additions & 2 deletions src/Tests/TestComponentCSharp/TestComponentCSharp.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@
</ClInclude>
<ClInclude Include="NonAgileClass.h" />
<ClInclude Include="ComImports.h" />
<ClInclude Include="Singleton.h" />
<ClInclude Include="WarningClass.h" />
<ClInclude Include="WarningStatic.h" />
</ItemGroup>
Expand All @@ -85,7 +84,6 @@
<ClCompile Include="NonAgileClass.cpp" />
<ClCompile Include="ComImports.cpp" />
<ClCompile Include="ManualProjectionTestClasses.cpp" />
<ClCompile Include="Singleton.cpp" />
<ClCompile Include="WarningClass.cpp" />
<ClCompile Include="WarningStatic.cpp" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
<ClCompile Include="ManualProjectionTestClasses.cpp" />
<ClCompile Include="WarningClass.cpp" />
<ClCompile Include="WarningStatic.cpp" />
<ClCompile Include="Singleton.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="pch.h" />
Expand All @@ -24,7 +23,6 @@
<ClInclude Include="ManualProjectionTestClasses.h" />
<ClInclude Include="WarningStatic.h" />
<ClInclude Include="WarningClass.h" />
<ClInclude Include="Singleton.h" />
</ItemGroup>
<ItemGroup>
<Midl Include="TestComponentCSharp.idl" />
Expand Down
26 changes: 5 additions & 21 deletions src/Tests/UnitTest/TestComponentCSharp_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2438,14 +2438,15 @@ public void TestCovariance()
Assert.True(TestObject.IterableOfObjectIterablesProperty.SequenceEqual(listOfListOfUris));
}

// Ensure that event subscription state is properly cached to enable later unsubscribes
[Fact]
public void TestEventSourceCaching()
public void TestStaticEventWithGC()
{
bool eventCalled = false;
void Class_StaticIntPropertyChanged(object sender, int e) => eventCalled = (e == 3);
void Class_StaticIntPropertyChanged(object sender, int e)
{
eventCalled = (e == 3);
}

// Test static codegen-based EventSource caching
Class.StaticIntPropertyChanged += Class_StaticIntPropertyChanged;
GC.Collect(2, GCCollectionMode.Forced, true);
GC.WaitForPendingFinalizers();
Expand All @@ -2457,23 +2458,6 @@ public void TestEventSourceCaching()
GC.WaitForPendingFinalizers();
Class.StaticIntProperty = 3;
Assert.True(eventCalled);

// Test dynamic WeakRef-based EventSource caching
eventCalled = false;
static void Subscribe(EventHandler<int> handler) => Singleton.Instance.IntPropertyChanged += handler;
static void Unsubscribe(EventHandler<int> handler) => Singleton.Instance.IntPropertyChanged -= handler;
static void Assign(int value) => Singleton.Instance.IntProperty = value;
Subscribe(Class_StaticIntPropertyChanged);
GC.Collect(2, GCCollectionMode.Forced, true);
GC.WaitForPendingFinalizers();
Unsubscribe(Class_StaticIntPropertyChanged);
Assign(3);
Assert.False(eventCalled);
Subscribe(Class_StaticIntPropertyChanged);
GC.Collect(2, GCCollectionMode.Forced, true);
GC.WaitForPendingFinalizers();
Assign(3);
Assert.True(eventCalled);
}

#if NET5_0
Expand Down
5 changes: 0 additions & 5 deletions src/WinRT.Runtime/ComWrappersSupport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,6 @@ public static void MarshalDelegateInvoke<T>(IntPtr thisPtr, Action<T> invoke)

public static IObjectReference GetObjectReferenceForInterface(IntPtr externalComObject)
{
if (externalComObject == IntPtr.Zero)
{
return null;
}

using var unknownRef = ObjectReference<IUnknownVftbl>.FromAbi(externalComObject);

if (IsFreeThreaded())
Expand Down
4 changes: 2 additions & 2 deletions src/WinRT.Runtime/Interop/IWeakReferenceSource.net5.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ namespace WinRT.Interop
{
[WindowsRuntimeType]
[Guid("00000038-0000-0000-C000-000000000046")]
public interface IWeakReferenceSource
internal interface IWeakReferenceSource
{
IWeakReference GetWeakReference();
}

[WindowsRuntimeType]
[Guid("00000037-0000-0000-C000-000000000046")]
public interface IWeakReference
internal interface IWeakReference
{
IObjectReference Resolve(Guid riid);
}
Expand Down
22 changes: 20 additions & 2 deletions src/WinRT.Runtime/Interop/IWeakReferenceSource.netstandard2.0.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ namespace WinRT.Interop
{
[WindowsRuntimeType]
[Guid("00000038-0000-0000-C000-000000000046")]
public interface IWeakReferenceSource
internal interface IWeakReferenceSource
{
IWeakReference GetWeakReference();
}

[WindowsRuntimeType]
[Guid("00000037-0000-0000-C000-000000000046")]
public interface IWeakReference
internal interface IWeakReference
{
IObjectReference Resolve(Guid riid);
}
Expand Down Expand Up @@ -61,19 +61,28 @@ internal struct Vftbl
public static readonly Vftbl AbiToProjectionVftable;
public static readonly IntPtr AbiToProjectionVftablePtr;

#if NETSTANDARD2_0
internal delegate int GetWeakReferenceDelegate(IntPtr thisPtr, IntPtr* weakReference);
private static readonly Delegate[] DelegateCache = new Delegate[1];
#endif
static Vftbl()
{
AbiToProjectionVftable = new Vftbl
{
IUnknownVftbl = global::WinRT.Interop.IUnknownVftbl.AbiToProjectionVftbl,
#if NETSTANDARD2_0
_GetWeakReference = Marshal.GetFunctionPointerForDelegate(DelegateCache[0] = new GetWeakReferenceDelegate(Do_Abi_GetWeakReference)).ToPointer(),
#else
_GetWeakReference = (delegate* unmanaged<IntPtr, IntPtr*, int>)&Do_Abi_GetWeakReference
#endif
};
AbiToProjectionVftablePtr = Marshal.AllocHGlobal(Marshal.SizeOf<Vftbl>());
Marshal.StructureToPtr(AbiToProjectionVftable, AbiToProjectionVftablePtr, false);
}

#if !NETSTANDARD2_0
[UnmanagedCallersOnly]
#endif
private static int Do_Abi_GetWeakReference(IntPtr thisPtr, IntPtr* weakReference)
{
*weakReference = default;
Expand Down Expand Up @@ -132,19 +141,28 @@ public struct Vftbl
public static readonly Vftbl AbiToProjectionVftable;
public static readonly IntPtr AbiToProjectionVftablePtr;

#if NETSTANDARD2_0
public delegate int ResolveDelegate(IntPtr thisPtr, Guid* riid, IntPtr* objectReference);
private static readonly Delegate[] DelegateCache = new Delegate[1];
#endif
static Vftbl()
{
AbiToProjectionVftable = new Vftbl
{
IUnknownVftbl = global::WinRT.Interop.IUnknownVftbl.AbiToProjectionVftbl,
#if NETSTANDARD2_0
_Resolve = Marshal.GetFunctionPointerForDelegate(DelegateCache[0] = new ResolveDelegate(Do_Abi_Resolve)).ToPointer(),
#else
_Resolve = (delegate* unmanaged<IntPtr, Guid*, IntPtr*, int>)&Do_Abi_Resolve
#endif
};
AbiToProjectionVftablePtr = Marshal.AllocHGlobal(Marshal.SizeOf<Vftbl>());
Marshal.StructureToPtr(AbiToProjectionVftable, AbiToProjectionVftablePtr, false);
}

#if !NETSTANDARD2_0
[UnmanagedCallersOnly]
#endif
private static int Do_Abi_Resolve(IntPtr thisPtr, Guid* riid, IntPtr* objectReference)
{
IObjectReference _objectReference = default;
Expand Down
Loading

0 comments on commit 0f079e6

Please sign in to comment.