-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
[core] fix memory leaks in bindings #13327
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -635,7 +635,7 @@ public void SubscribeTo(INotifyPropertyChanged source, PropertyChangedEventHandl | |
_listener.SetTarget(listener); | ||
} | ||
|
||
public void Unsubscribe() | ||
public void Unsubscribe(bool finalizer = false) | ||
{ | ||
INotifyPropertyChanged source; | ||
if (_source.TryGetTarget(out source) && source != null) | ||
|
@@ -644,6 +644,10 @@ public void Unsubscribe() | |
if (bo != null) | ||
bo.BindingContextChanged -= _bchandler; | ||
|
||
// If we are called from a finalizer, WeakReference<T>.SetTarget() can throw InvalidOperationException | ||
if (finalizer) | ||
return; | ||
Comment on lines
+647
to
+649
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Next, I found |
||
|
||
_source.SetTarget(null); | ||
_listener.SetTarget(null); | ||
} | ||
|
@@ -668,6 +672,8 @@ class BindingExpressionPart | |
readonly PropertyChangedEventHandler _changeHandler; | ||
WeakPropertyChangedProxy _listener; | ||
|
||
~BindingExpressionPart() => _listener?.Unsubscribe(finalizer: true); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the core fix, that allows |
||
|
||
public BindingExpressionPart(BindingExpression expression, string content, bool isIndexer = false) | ||
{ | ||
_expression = expression; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,10 @@ | ||
using System; | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
using System.ComponentModel; | ||
using System.Globalization; | ||
using System.Linq; | ||
using System.Reflection; | ||
using System.Runtime.CompilerServices; | ||
using System.Threading.Tasks; | ||
using Xunit; | ||
|
@@ -1185,6 +1187,81 @@ public void ValueUpdatedWithSelfPathOnTwoWayBinding(bool isDefault) | |
AssertNoErrorsLogged(); | ||
} | ||
|
||
|
||
[Theory, Category("[Binding] Complex paths")] | ||
[InlineData(BindingMode.OneWay)] | ||
[InlineData(BindingMode.OneWayToSource)] | ||
[InlineData(BindingMode.TwoWay)] | ||
public async Task WeakPropertyChangedProxyDoesNotLeak(BindingMode mode) | ||
{ | ||
var proxies = new List<WeakReference>(); | ||
WeakReference weakViewModel = null, weakBindable = null; | ||
|
||
int i = 0; | ||
void create () | ||
{ | ||
if (i++ < 1024) | ||
{ | ||
create(); | ||
return; | ||
} | ||
|
||
var binding = new Binding("Model.Model[1]"); | ||
var bindable = new MockBindable(); | ||
weakBindable = new WeakReference(bindable); | ||
|
||
var viewmodel = new ComplexMockViewModel | ||
{ | ||
Model = new ComplexMockViewModel | ||
{ | ||
Model = new ComplexMockViewModel() | ||
} | ||
}; | ||
|
||
weakViewModel = new WeakReference(viewmodel); | ||
|
||
bindable.BindingContext = viewmodel; | ||
bindable.SetBinding(MockBindable.TextProperty, binding); | ||
|
||
// Access private members: | ||
// WeakPropertyChangedProxy proxy = binding._expression._parts[i]._listener; | ||
var flags = BindingFlags.NonPublic | BindingFlags.Instance; | ||
var expression = binding.GetType().GetField("_expression", flags).GetValue(binding); | ||
Assert.NotNull(expression); | ||
var parts = expression.GetType().GetField("_parts", flags).GetValue(expression) as IEnumerable; | ||
Assert.NotNull(parts); | ||
foreach (var part in parts) | ||
{ | ||
var listener = part.GetType().GetField("_listener", flags).GetValue(part); | ||
if (listener == null) | ||
continue; | ||
proxies.Add(new WeakReference(listener)); | ||
} | ||
Assert.NotEmpty(proxies); // Should be at least 1 | ||
}; | ||
create(); | ||
|
||
await Task.Yield(); | ||
GC.Collect(); | ||
GC.WaitForPendingFinalizers(); | ||
|
||
if (mode == BindingMode.TwoWay || mode == BindingMode.OneWay) | ||
Assert.False(weakViewModel.IsAlive, "ViewModel wasn't collected"); | ||
Comment on lines
+1248
to
+1249
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Before I changed |
||
|
||
if (mode == BindingMode.TwoWay || mode == BindingMode.OneWayToSource) | ||
Assert.False(weakBindable.IsAlive, "Bindable wasn't collected"); | ||
|
||
// WeakPropertyChangedProxy won't go away until the second GC, BindingExpressionPart unsubscribes in its finalizer | ||
await Task.Yield(); | ||
GC.Collect(); | ||
GC.WaitForPendingFinalizers(); | ||
|
||
foreach (var proxy in proxies) | ||
{ | ||
Assert.False(proxy.IsAlive, "WeakPropertyChangedProxy wasn't collected"); | ||
} | ||
} | ||
|
||
[Theory, Category("[Binding] Complex paths")] | ||
[InlineData(BindingMode.OneWay)] | ||
[InlineData(BindingMode.OneWayToSource)] | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Lastly, I found that
VisualElement.BackgroundProperty.BindingContext
held an instance of aViewModel
. This was the value ofBrush.Default._inheritedContext
.