Skip to content

Commit

Permalink
Update ResolverDebugProperties to avoid keeping strong refs to any se…
Browse files Browse the repository at this point in the history
…rvice instance
  • Loading branch information
gustavopsantos committed Sep 2, 2024
1 parent 329cf4d commit 5f16ff2
Show file tree
Hide file tree
Showing 6 changed files with 8 additions and 21 deletions.
7 changes: 0 additions & 7 deletions Assets/Reflex.EditModeTests/GarbageCollectionTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using FluentAssertions;
using NUnit.Framework;
Expand All @@ -22,12 +21,6 @@ public static void ForceGarbageCollection()
GC.WaitForPendingFinalizers();
}

[Conditional("REFLEX_DEBUG")]
public static void MarkAsInconclusiveWhenReflexDebugIsEnabled()
{
Assert.Inconclusive("Disable REFLEX_DEBUG symbol when running garbage collection tests!");
}

[Test, Retry(3)]
public void Singleton_ShouldBeFinalized_WhenOwnerIsDisposed()
{
Expand Down
4 changes: 0 additions & 4 deletions Assets/Reflex.EditModeTests/ScopedTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,6 @@ public void ScopedFromFactory_ConstructedInstances_ShouldBeDisposed_WithinConstr
[Test, Retry(3)]
public void ScopedFromType_ConstructedInstances_ShouldBeCollected_WhenConstructingContainerIsDisposed()
{
GarbageCollectionTests.MarkAsInconclusiveWhenReflexDebugIsEnabled();

WeakReference instanceConstructedByChild;
WeakReference instanceConstructedByParent;
var parentContainer = new ContainerBuilder().AddScoped(typeof(Service)).Build();
Expand All @@ -120,8 +118,6 @@ void Act()
[Test, Retry(3)]
public void ScopedFromFactory_ConstructedInstances_ShouldBeCollected_WhenConstructingContainerIsDisposed()
{
GarbageCollectionTests.MarkAsInconclusiveWhenReflexDebugIsEnabled();

WeakReference instanceConstructedByChild;
WeakReference instanceConstructedByParent;
var parentContainer = new ContainerBuilder().AddScoped(_ => new Service()).Build();
Expand Down
4 changes: 0 additions & 4 deletions Assets/Reflex.EditModeTests/TransientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,6 @@ public void TransientFromFactory_ConstructedInstances_ShouldBeDisposed_WithinCon
[Test, Retry(3)]
public void TransientFromType_ConstructedInstances_ShouldBeCollected_WhenConstructingContainerIsDisposed()
{
GarbageCollectionTests.MarkAsInconclusiveWhenReflexDebugIsEnabled();

WeakReference instanceConstructedByChild;
WeakReference instanceConstructedByParent;
var parentContainer = new ContainerBuilder().AddTransient(typeof(Service)).Build();
Expand All @@ -74,8 +72,6 @@ void Act()
[Test, Retry(3)]
public void TransientFromFactory_ConstructedInstances_ShouldBeCollected_WhenConstructingContainerIsDisposed()
{
GarbageCollectionTests.MarkAsInconclusiveWhenReflexDebugIsEnabled();

WeakReference instanceConstructedByChild;
WeakReference instanceConstructedByParent;
var parentContainer = new ContainerBuilder().AddTransient(c => new Service()).Build();
Expand Down
5 changes: 3 additions & 2 deletions Assets/Reflex/Diagnosis/Diagnosis.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using Reflex.Core;
Expand All @@ -19,7 +20,7 @@ internal static void IncrementResolutions(IResolver resolver)
[Conditional("REFLEX_DEBUG")]
internal static void RegisterInstance(IResolver resolver, object instance)
{
resolver.GetDebugProperties().Instances.Add((instance, GetCallSite(3)));
resolver.GetDebugProperties().Instances.Add((new WeakReference(instance), GetCallSite(3)));
}

[Conditional("REFLEX_DEBUG")]
Expand Down
4 changes: 2 additions & 2 deletions Assets/Reflex/Editor/DebuggingWindow/ReflexDebuggerWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -165,10 +165,10 @@ private void BuildDataRecursively(MyTreeElement parent, Container container)
kind: pair.Item1.GetType().Name.Replace("Singleton", string.Empty).Replace("Transient", string.Empty).Replace("Scoped", string.Empty).Replace("Resolver", string.Empty)
);

foreach (var (instance, callsite) in pair.Item1.GetDebugProperties().Instances)
foreach (var (instance, callsite) in pair.Item1.GetDebugProperties().Instances.Where(tuple => tuple.Item1.IsAlive).Select(tuple => (tuple.Item1.Target, tuple.Item2)))
{
var instanceTreeElement = new MyTreeElement(
$"{instance.GetType().GetName()} <b><color=#3D99ED>({SHA1.ShortHash(instance.GetHashCode())})</color></b>",
$"{instance.GetType().GetName()} <color=#3D99ED>({SHA1.ShortHash(instance.GetHashCode())})</color>",
resolverTreeElement.Depth + 1,
++_id,
InstanceIcon,
Expand Down
5 changes: 3 additions & 2 deletions Assets/Reflex/Resolvers/ResolverDebugProperties.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;

namespace Reflex.Resolvers
{
public sealed class ResolverDebugProperties
{
public int Resolutions;
public List<(object, List<CallSite>)> Instances { get; } = new();
public List<(WeakReference, List<CallSite>)> Instances { get; } = new();
public List<CallSite> BindingCallsite { get; } = new();
}
}

0 comments on commit 5f16ff2

Please sign in to comment.