Skip to content

Commit

Permalink
Fix Repeater memoization memory leak
Browse files Browse the repository at this point in the history
If the ConditionalWeakTable references itself, it will get leaked by
.NET Core GC: dotnet/runtime#12255

Fortunately, we don't really need the ConditinalWeakTable here:
 * Repeater should generally be a short-lived object, so
       strong references will also be collected reasonably soon
 * Between Load and PreRender, the viewmodels were pinned
       in the DataContext of all children and the children are in Children
       collection. Only after PreRender could those references be useful,
       but we can as well Clear the dictionary at that point.

    Co-authored-by: Adam Štěpánek <[email protected]>
  • Loading branch information
exyi committed Dec 1, 2024
1 parent b1771cc commit 5e3945e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
13 changes: 10 additions & 3 deletions src/Framework/Framework/Controls/Repeater.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
using System.Diagnostics;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Collections.Generic;
using DotVVM.Framework.Utils;

namespace DotVVM.Framework.Controls
{
Expand Down Expand Up @@ -138,6 +140,7 @@ protected internal override void OnLoad(IDotvvmRequestContext context)
protected internal override void OnPreRender(IDotvvmRequestContext context)
{
SetChildren(context, renderClientTemplate: !RenderOnServer, memoizeReferences: false);
childrenCache.Clear(); // not going to need the unreferenced children anymore
base.OnPreRender(context);
}

Expand Down Expand Up @@ -246,7 +249,7 @@ private DotvvmControl GetEmptyItem(IDotvvmRequestContext context)
return emptyDataContainer;
}

private ConditionalWeakTable<object, DataItemContainer> childrenCache = new ConditionalWeakTable<object, DataItemContainer>();
private readonly Dictionary<object, DataItemContainer> childrenCache = new(ReferenceEqualityComparer<object>.Instance);
private DotvvmControl GetItem(IDotvvmRequestContext context, object? item = null, int? index = null, bool allowMemoizationRetrieve = false, bool allowMemoizationStore = false)
{
if (allowMemoizationRetrieve && item != null && childrenCache.TryGetValue(item, out var container2) && container2.Parent == null)
Expand All @@ -272,8 +275,12 @@ private DotvvmControl GetItem(IDotvvmRequestContext context, object? item = null
// write it to the cache after the content is build. If it would be before that, exception could be suppressed
if (allowMemoizationStore && item != null)
{
// this GetValue call adds the value without raising exception when the value is already added...
childrenCache.GetValue(item, _ => container);
#if DotNetCore
childrenCache.TryAdd(item, container);
#else
if (!childrenCache.ContainsKey(item))
childrenCache.Add(item, container);
#endif
}

return container;
Expand Down
16 changes: 16 additions & 0 deletions src/Framework/Framework/Utils/ReferenceEqualityComparer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System.Collections.Generic;
using System.Runtime.CompilerServices;

namespace DotVVM.Framework.Utils
{
// TODO next version: Replace with System.Collections.Generic.ReferenceEqualityComparer
internal class ReferenceEqualityComparer<T> : IEqualityComparer<T>
where T : class
{
public static ReferenceEqualityComparer<T> Instance { get; } = new ReferenceEqualityComparer<T>();


public bool Equals(T? x, T? y) => ReferenceEquals(x, y);
public int GetHashCode(T obj) => obj == null ? 0 : RuntimeHelpers.GetHashCode(obj);
}
}

0 comments on commit 5e3945e

Please sign in to comment.