Skip to content

Commit

Permalink
Binding leak
Browse files Browse the repository at this point in the history
  • Loading branch information
Paymon Khamooshi committed Feb 14, 2024
1 parent 50bcb73 commit c6179e2
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 66 deletions.
2 changes: 1 addition & 1 deletion Olive/-Extensions/Linq.cs
Original file line number Diff line number Diff line change
Expand Up @@ -816,7 +816,7 @@ public static void Remove<T>(this IList<T> @this, IEnumerable<T> itemsToRemove)
if (itemsToRemove != null)
{
foreach (var item in itemsToRemove)
if (@this.Contains(item)) @this.Remove(item);
@this.Remove(item);
}
}

Expand Down
8 changes: 3 additions & 5 deletions Olive/Bindable/Bindable-TVAlue.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
namespace Olive
{
using System;
using System.Linq;
using System.Reflection;

/// <summary>
Expand Down Expand Up @@ -121,18 +122,15 @@ public IBinding<TValue> AddBinding<TProperty>(object target, string propertyName

IBinding<TValue> Add(PropertyBinding<TValue> binding)
{
Bindings
.FirstOrDefault(x => x.Equals(binding))?
.Remove();
Bindings.RemoveWhere(x => x is PropertyBinding<TValue> b && (b.IsDead() || b.SameTarget(binding)));

Bindings.Add(binding);
binding.Apply(value);

return binding;
}

internal void RemoveBinding(PropertyBinding<TValue> binding)
=> Bindings.Remove(binding);
internal void RemoveBinding(PropertyBinding<TValue> binding) => Bindings.Remove(binding);

static PropertyInfo FindProperty(object target, string propertyName)
{
Expand Down
37 changes: 19 additions & 18 deletions Olive/Bindable/PropertyBinding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,19 @@
using System;
using System.Reflection;

public interface IMortal
{
bool IsDead();
}

internal struct PropertyBinding<TValue> : IBinding<TValue>
{
Bindable<TValue> Owner;

public WeakReference<object> Target;
public PropertyInfo Property;
public Func<TValue, object> Expression;

bool IsRemoved;

public PropertyBinding(Bindable<TValue> owner) => Owner = owner;
Expand Down Expand Up @@ -44,7 +49,15 @@ public void Apply(TValue value)
public void Remove()
{
Owner.RemoveBinding(this);
IsRemoved = true; // How about clean up?
IsRemoved = true;
}

public bool IsDead()
{
if (IsRemoved) return true;
if (!Target.TryGetTarget(out var target)) return true;
if (target is IMortal m && m.IsDead()) return true;
return false;
}

object GetSettableValue(TValue rawValue)
Expand All @@ -55,25 +68,13 @@ object GetSettableValue(TValue rawValue)
return rawValue;
}

public override bool Equals(object obj)
internal bool SameTarget(PropertyBinding<TValue> binding)
{
if (obj is not PropertyBinding<TValue> binding) return false;

if (binding.IsRemoved != IsRemoved) return false;
if (binding.Property != Property) return false;
if (!binding.Target.TryGetTarget(out var target)) return false;
if (target is null) return false;
if (!Target.TryGetTarget(out var myTarget)) return false;

if (Target.TryGetTarget(out var myTarget))
return ReferenceEquals(myTarget, target);

return false;
}

public override int GetHashCode()
{
return (Target.TryGetTarget(out var myTarget) ? myTarget?.GetHashCode() : -1)
?? base.GetHashCode();
return ReferenceEquals(myTarget, target);
}
}
}
88 changes: 46 additions & 42 deletions Olive/Olive.csproj
Original file line number Diff line number Diff line change
@@ -1,44 +1,48 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<Version>2.1.360.0</Version>
<OutputType>Library</OutputType>
<LangVersion>latest</LangVersion>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<DocumentationFile>$(TargetDir)\Olive.xml</DocumentationFile>
<DefineConstants>TRACE;ProfileGCop</DefineConstants>
</PropertyGroup>
<ItemGroup>
<None Update="-Extensions\Async\Linq.SpecialCollectionTypes.Async.tt">
<LastGenOutput>Linq.SpecialCollectionTypes.Async.cs</LastGenOutput>
<Generator>TextTemplatingFileGenerator</Generator>
</None>
<None Update="-Extensions\Async\Linq.NumericIEnumerable.Async.tt">
<Generator>TextTemplatingFileGenerator</Generator>
<LastGenOutput>Linq.NumericIEnumerable.Async.cs</LastGenOutput>
</None>
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="7.0.4" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="7.0.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
</ItemGroup>
<ItemGroup>
<Service Include="{508349b6-6b84-4df5-91f0-309beebad82d}" />
</ItemGroup>
<ItemGroup>
<Compile Update="-Extensions\Async\Linq.NumericIEnumerable.Async.cs">
<DesignTime>True</DesignTime>
<AutoGen>True</AutoGen>
<DependentUpon>Linq.NumericIEnumerable.Async.tt</DependentUpon>
</Compile>
<Compile Update="-Extensions\Async\Linq.SpecialCollectionTypes.Async.cs">
<DesignTime>True</DesignTime>
<AutoGen>True</AutoGen>
<DependentUpon>Linq.SpecialCollectionTypes.Async.tt</DependentUpon>
</Compile>
</ItemGroup>
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<Version>2.1.361.0</Version>
<OutputType>Library</OutputType>
<LangVersion>latest</LangVersion>
<PackageId>Olive</PackageId>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<DocumentationFile>$(TargetDir)\Olive.xml</DocumentationFile>
<DefineConstants>TRACE;ProfileGCop</DefineConstants>
</PropertyGroup>
<ItemGroup>
<None Update="-Extensions\Async\Linq.SpecialCollectionTypes.Async.tt">
<LastGenOutput>Linq.SpecialCollectionTypes.Async.cs</LastGenOutput>
<Generator>TextTemplatingFileGenerator</Generator>
</None>
<None Update="-Extensions\Async\Linq.NumericIEnumerable.Async.tt">
<Generator>TextTemplatingFileGenerator</Generator>
<LastGenOutput>Linq.NumericIEnumerable.Async.cs</LastGenOutput>
</None>
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="7.0.4" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="7.0.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
</ItemGroup>
<ItemGroup>
<Service Include="{508349b6-6b84-4df5-91f0-309beebad82d}" />
</ItemGroup>
<ItemGroup>
<Compile Update="-Extensions\Async\Linq.NumericIEnumerable.Async.cs">
<DesignTime>True</DesignTime>
<AutoGen>True</AutoGen>
<DependentUpon>Linq.NumericIEnumerable.Async.tt</DependentUpon>
</Compile>
<Compile Update="-Extensions\Async\Linq.SpecialCollectionTypes.Async.cs">
<DesignTime>True</DesignTime>
<AutoGen>True</AutoGen>
<DependentUpon>Linq.SpecialCollectionTypes.Async.tt</DependentUpon>
</Compile>
</ItemGroup>
<Target Name="PostBuild" AfterTargets="PostBuildEvent">
<Exec Command="update-local-nuget-cache $(TargetDir)" />
</Target>
</Project>

0 comments on commit c6179e2

Please sign in to comment.