'Why C# Expressions Lambda cause memory leak?
I have created the following extension method:
public static IQueryable<StudioClass> FilterByIdentifiers(this IQueryable<StudioClass> classes, Tuple<Guid,DateTime?>[] identifiers)
{
if (identifiers != null)
{
var parameter = Expression.Parameter(typeof(StudioClass), "sc");
var idMemberAccess = Expression.Property(parameter, "Id");
var dateMemberAccess = Expression.Property(parameter, "DateCreatedUTC");
//False empty expression
var expr = Expression.Equal(Expression.Constant(2), Expression.Constant(1));
foreach (var (identifier, timestamp) in identifiers)
{
if (timestamp.HasValue)
{
var complexExpression = Expression.AndAlso(Expression.Equal(idMemberAccess, Expression.Constant(identifier)),
Expression.Equal(dateMemberAccess, Expression.Constant(timestamp.Value)));
expr = Expression.OrElse(expr, complexExpression);
}
else
{
expr = Expression.OrElse(expr, Expression.Equal(idMemberAccess, Expression.Constant(identifier)));
}
}
var lambda = (Expression<Func<StudioClass, bool>>)Expression.Lambda(expr, parameter);
classes = classes.Where(lambda);
}
return classes;
}
We have identified that this creates a memory leak, and I can't understand why. Hope somebody can help me identify the problem or point me to some resource of why this might be happening.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
