Skip to content

Commit

Permalink
Fix memory leak
Browse files Browse the repository at this point in the history
  • Loading branch information
oformaniuk committed Jan 24, 2022
1 parent 5f4bf50 commit c127e41
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,25 @@ internal class ClosureExpressionMiddleware : IExpressionMiddleware
{
public Expression<T> Invoke<T>(Expression<T> expression) where T : Delegate
{
using var container = GenericObjectPool<List<ConstantExpression>>.Shared.Use();
var constants = container.Value;
var constants = new List<ConstantExpression>();
var closureCollectorVisitor = new ClosureCollectorVisitor(constants);
expression = (Expression<T>) closureCollectorVisitor.Visit(expression);

if (constants.Count == 0) return expression;

using var closureBuilder = ClosureBuilder.Create();
for (var index = 0; index < constants.Count; index++)

KeyValuePair<ParameterExpression, Dictionary<Expression, Expression>> closureDefinition;
Closure closure;
using (var closureBuilder = ClosureBuilder.Create())
{
var value = constants[index];
closureBuilder.Add(value);
for (var index = 0; index < constants.Count; index++)
{
var value = constants[index];
closureBuilder.Add(value);
}

closureDefinition = closureBuilder.Build(out closure);
}

var closureDefinition = closureBuilder.Build(out var closure);

var closureVisitor = new ClosureVisitor(closureDefinition);
expression = (Expression<T>) closureVisitor.Visit(expression);

Expand Down
9 changes: 3 additions & 6 deletions source/Handlebars/Pools/GenericObjectPool.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
using System;

namespace HandlebarsDotNet.Pools
namespace HandlebarsDotNet.Pools
{
internal class GenericObjectPool<T> : InternalObjectPool<T, GenericObjectPool<T>.Policy>
where T : class, new()
{
private static readonly Lazy<GenericObjectPool<T>> Instance = new(() => new GenericObjectPool<T>());
public static GenericObjectPool<T> Shared => Instance.Value;

public static GenericObjectPool<T> Shared { get; } = new();

private GenericObjectPool() : base(new Policy()) { }

public readonly struct Policy : IInternalObjectPoolPolicy<T>
Expand Down

0 comments on commit c127e41

Please sign in to comment.