summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Core.UnitTests
diff options
context:
space:
mode:
authorJonathan Peppers <jonathan.peppers@gmail.com>2016-08-16 13:19:21 -0500
committerJason Smith <jason.smith@xamarin.com>2016-08-16 11:19:21 -0700
commitf6febd4c81a80430331c7fcdcc63271aa4fa636c (patch)
tree01445df8cb4cfbc13e49356eb80fd8810a9df474 /Xamarin.Forms.Core.UnitTests
parent30c0dcb949186c21c60c4c9ddf8a581d40a43662 (diff)
downloadxamarin-forms-f6febd4c81a80430331c7fcdcc63271aa4fa636c.tar.gz
xamarin-forms-f6febd4c81a80430331c7fcdcc63271aa4fa636c.tar.bz2
xamarin-forms-f6febd4c81a80430331c7fcdcc63271aa4fa636c.zip
Fix for BindingExpression memory leak (#279)
* Unit test proving a memory leak with Binding What we were seeing in our app was that Binding objects stay around when bound to long-lived ViewModels, even when the View is long gone * BindingExpression - INotifyPropertyChanged should use WeakReference I had to make a WeakPropertyChangedProxy class for this, I could not think of a way to get around creating a new object for this
Diffstat (limited to 'Xamarin.Forms.Core.UnitTests')
-rw-r--r--Xamarin.Forms.Core.UnitTests/BindingUnitTests.cs27
1 files changed, 27 insertions, 0 deletions
diff --git a/Xamarin.Forms.Core.UnitTests/BindingUnitTests.cs b/Xamarin.Forms.Core.UnitTests/BindingUnitTests.cs
index c520e929..c28adda9 100644
--- a/Xamarin.Forms.Core.UnitTests/BindingUnitTests.cs
+++ b/Xamarin.Forms.Core.UnitTests/BindingUnitTests.cs
@@ -5,6 +5,7 @@ using System.Globalization;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
+using System.Threading.Tasks;
using NUnit.Framework;
using CategoryAttribute=NUnit.Framework.CategoryAttribute;
using DescriptionAttribute=NUnit.Framework.DescriptionAttribute;
@@ -2592,6 +2593,32 @@ namespace Xamarin.Forms.Core.UnitTests
}
[Test]
+ public async Task BindingDoesNotStayAliveForDeadTarget()
+ {
+ TestViewModel viewModel = new TestViewModel();
+ WeakReference bindingRef;
+
+ {
+ var binding = new Binding("Foo");
+
+ var button = new Button();
+ button.SetBinding(Button.TextProperty, binding);
+ button.BindingContext = viewModel;
+
+ bindingRef = new WeakReference(binding);
+ }
+
+ Assume.That(viewModel.InvocationListSize(), Is.EqualTo(1));
+
+ //NOTE: this was the only way I could "for sure" get the binding to get GC'd
+ GC.Collect();
+ await Task.Delay(10);
+ GC.Collect();
+
+ Assert.IsFalse(bindingRef.IsAlive, "Binding should not be alive!");
+ }
+
+ [Test]
public void BindingCreatesSingleSubscription ()
{
TestViewModel viewmodel = new TestViewModel();