Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[controls] one less WeakReference in TypedBinding #13304

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 9 additions & 10 deletions src/Controls/src/Core/TypedBinding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -249,29 +249,28 @@ class PropertyChangedProxy
public Func<TSource, object> PartGetter { get; }
public string PropertyName { get; }
public BindingExpression.WeakPropertyChangedProxy Listener { get; }
WeakReference<INotifyPropertyChanged> _weakPart = new WeakReference<INotifyPropertyChanged>(null);
readonly BindingBase _binding;
PropertyChangedEventHandler handler;
public INotifyPropertyChanged Part
{
get
{
INotifyPropertyChanged target;
if (_weakPart.TryGetTarget(out target))
if (Listener != null && Listener.Source.TryGetTarget(out var target))
return target;
return null;
}
set
{
if (Listener != null && Listener.Source.TryGetTarget(out var source) && ReferenceEquals(value, source))
if (Listener != null)
{
//Already subscribed
return;

//clear out previous subscription
Listener?.Unsubscribe();
if (Listener.Source.TryGetTarget(out var source) && ReferenceEquals(value, source))
return;

_weakPart.SetTarget(value);
Listener.SubscribeTo(value, handler);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line actually looked like it could throw NRE before?

//clear out previous subscription
Listener.Unsubscribe();
Listener.SubscribeTo(value, handler);
}
}
}

Expand Down
42 changes: 33 additions & 9 deletions src/Controls/tests/Core.UnitTests/TypedBindingUnitTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Globalization;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using Microsoft.Maui.Controls.Internals;
using Xunit;
using Xunit.Sdk;
Expand Down Expand Up @@ -1376,7 +1377,20 @@ event PropertyChangedEventHandler INotifyPropertyChanged.PropertyChanged
remove { PropertyChanged -= value; }
}

public string Foo { get; set; }
string _foo;

public string Foo
{
get => _foo;
set
{
if (_foo != value)
{
_foo = value;
OnPropertyChanged(nameof(Foo));
}
}
}

public int InvocationListSize()
{
Expand All @@ -1392,9 +1406,10 @@ public virtual void OnPropertyChanged([CallerMemberName] string propertyName = n
}

[Fact]
public void BindingUnsubscribesForDeadTarget()
public async Task BindingUnsubscribesForDeadTarget()
{
var viewmodel = new TestViewModel();
WeakReference bindingRef = null, buttonRef = null;

int i = 0;
Action create = null;
Expand All @@ -1407,30 +1422,37 @@ public void BindingUnsubscribesForDeadTarget()
}

var button = new Button();
button.SetBinding(Button.TextProperty, new TypedBinding<TestViewModel, string>(vm => (vm.Foo, true), (vm, s) => vm.Foo = s, new[] {
var binding = new TypedBinding<TestViewModel, string>(vm => (vm.Foo, true), (vm, s) => vm.Foo = s, new[] {
new Tuple<Func<TestViewModel, object>, string>(vm=>vm,"Foo")
}));
});
button.SetBinding(Button.TextProperty, binding);
button.BindingContext = viewmodel;
bindingRef = new WeakReference(binding);
buttonRef = new WeakReference(button);
};

create();
Assert.Equal(viewmodel.Foo = "Bar", ((Button)buttonRef.Target).Text);

Assert.Equal(1, viewmodel.InvocationListSize());

await Task.Yield();
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();

viewmodel.OnPropertyChanged("Foo");

Assert.Equal(0, viewmodel.InvocationListSize());

Assert.False(bindingRef.IsAlive, "Binding should not be alive!");
Assert.False(buttonRef.IsAlive, "Button should not be alive!");
}

[Fact]
public void BindingDoesNotStayAliveForDeadTarget()
public async Task BindingDoesNotStayAliveForDeadTarget()
{
var viewModel = new TestViewModel();
WeakReference bindingRef = null;
WeakReference bindingRef = null, buttonRef = null;

int i = 0;
Action create = null;
Expand All @@ -1450,18 +1472,20 @@ public void BindingDoesNotStayAliveForDeadTarget()
button.BindingContext = viewModel;

bindingRef = new WeakReference(binding);
binding = null;
buttonRef = new WeakReference(button);
};

create();
Assert.Equal(viewModel.Foo = "Bar", ((Button)buttonRef.Target).Text);

Assert.Equal(1, viewModel.InvocationListSize());

await Task.Yield();
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();

Assert.False(bindingRef.IsAlive, "Binding should not be alive!");
Assert.False(buttonRef.IsAlive, "Button should not be alive!");
}

[Fact]
Expand Down