Skip to content
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.CompilerServices;

namespace Microsoft.Maui.Hosting.Internal
{
internal sealed class RegisteredHandlerServiceTypeSet
{
private static readonly ConcurrentDictionary<IMauiHandlersCollection, RegisteredHandlerServiceTypeSet> s_instances = new();
private static readonly ConditionalWeakTable<IMauiHandlersCollection, RegisteredHandlerServiceTypeSet> s_instances = new();

internal static RegisteredHandlerServiceTypeSet GetInstance(IMauiHandlersCollection collection) =>
s_instances.GetOrAdd(collection, static _ => new RegisteredHandlerServiceTypeSet());
s_instances.GetValue(collection, static _ => new RegisteredHandlerServiceTypeSet());

private readonly HashSet<Type> _concreteHandlerServiceTypeSet = new();
private readonly HashSet<Type> _interfaceHandlerServiceTypeSet = new();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using Microsoft.Maui.Hosting;
using Microsoft.Maui.Hosting.Internal;
using Microsoft.Maui.Platform;
using Microsoft.Maui.UnitTests;
Expand Down Expand Up @@ -142,6 +144,27 @@ public void ResolvesToHandlerRegisteredUnderConcreteTypeDespiteAmbiguousInterfac
Assert.Equal(typeof(ViewStub), resolvedServiceType);
}

[Fact]
public void RegisteredHandlerServiceCollectionCanBeCollected()
{
var collection = RegisterHandlerService();

GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();

Assert.False(collection.IsAlive);
}

[MethodImpl(MethodImplOptions.NoInlining)]
private static WeakReference RegisterHandlerService()
{
var collection = new MauiHandlersCollection();
collection.AddHandler<ViewStub, ViewHandlerStub>();

return new WeakReference(collection);
}

class ChildViewStub : ViewStub, IParentAViewStub, IParentBViewStub { }
interface IParentAViewStub : IViewStub { }
interface IParentBViewStub : IViewStub { }
Expand Down
Loading