From b3f6d80a1b0acdbd335c810b835e4597d94269b5 Mon Sep 17 00:00:00 2001 From: "E.Z. Hart" Date: Tue, 16 May 2017 11:27:41 -0600 Subject: [PATCH] Remove VisualElement finalizer (#918) * [Controls] Add repo for bugzilla 55365 * Remove finalizer from VisualElement * Remove unused using directive * Removing test for 44074 * Update docs --- .../Bugzilla55365.cs | 90 +++++++++++++++++++ ...rin.Forms.Controls.Issues.Shared.projitems | 1 + Xamarin.Forms.Core.UnitTests/BehaviorTest.cs | 37 -------- Xamarin.Forms.Core/VisualElement.cs | 13 --- .../Xamarin.Forms/VisualElement.xml | 16 ---- 5 files changed, 91 insertions(+), 66 deletions(-) create mode 100644 Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Bugzilla55365.cs diff --git a/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Bugzilla55365.cs b/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Bugzilla55365.cs new file mode 100644 index 000000000..1ab99a2ed --- /dev/null +++ b/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Bugzilla55365.cs @@ -0,0 +1,90 @@ +using System; +using System.Collections.ObjectModel; +using System.Collections.Specialized; +using Xamarin.Forms.CustomAttributes; +using Xamarin.Forms.Internals; +#if UITEST +using NUnit.Framework; + +#endif + +namespace Xamarin.Forms.Controls.Issues +{ + [Preserve(AllMembers = true)] + [Issue(IssueTracker.Bugzilla, 55365, "~VisualElement crashes with System.Runtime.InteropServices.COMException", PlatformAffected.UWP)] + public class Bugzilla55365 : TestContentPage + { + readonly StackLayout _itemsPanel = new StackLayout(); + readonly DataTemplate _itemTemplate = new DataTemplate(CreateBoxView); + readonly StackLayout _layout = new StackLayout(); + +#if UITEST + [Test] + public void ForcingGCDoesNotCrash() + { + RunningApp.WaitForElement("Clear"); + RunningApp.Tap("Clear"); + RunningApp.Tap("Garbage"); + RunningApp.WaitForElement("Success"); + } +#endif + + protected override void Init() + { + var viewModel = new ObservableCollection<_55365Item> + { + new _55365Item { Subject = 65 } + }; + + viewModel.CollectionChanged += OnCollectionChanged; + + _itemsPanel.BindingContext = viewModel; + + foreach (_55365Item item in viewModel) + { + _itemTemplate.SetValue(BindingContextProperty, item); + var view = (View)_itemTemplate.CreateContent(); + _itemsPanel.Children.Add(view); + } + + var clearButton = new Button { Text = "Clear", Command = new Command(o => viewModel.Clear()) }; + _layout.Children.Add(clearButton); + + var collectButton = new Button { Text = "Garbage", Command = new Command(o => + { + GC.Collect(); + GC.WaitForPendingFinalizers(); + _layout.Children.Add(new Label {Text = "Success"}); + }) }; + _layout.Children.Add(collectButton); + _layout.Children.Add(_itemsPanel); + + Content = _layout; + } + + static object CreateBoxView() + { + var boxView1 = new BoxView { HeightRequest = 100, Color = new Color(0.55, 0.23, 0.147) }; + var setter1 = new Setter { Property = BoxView.ColorProperty, Value = "#FF2879DD" }; + var trigger1 = new DataTrigger(typeof(BoxView)) { Binding = new Binding("Subject"), Value = 65 }; + trigger1.Setters.Add(setter1); + boxView1.Triggers.Add(trigger1); + return boxView1; + } + + void OnCollectionChanged(object sender, NotifyCollectionChangedEventArgs e) + { + if (e.Action == NotifyCollectionChangedAction.Reset) + { + // reset the list + _itemsPanel.Children.Clear(); + } + } + + [Preserve(AllMembers = true)] + public class _55365Item + { + public int Subject { get; set; } + } + } +} \ No newline at end of file diff --git a/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Xamarin.Forms.Controls.Issues.Shared.projitems b/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Xamarin.Forms.Controls.Issues.Shared.projitems index 00b503010..1e63bab42 100644 --- a/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Xamarin.Forms.Controls.Issues.Shared.projitems +++ b/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Xamarin.Forms.Controls.Issues.Shared.projitems @@ -282,6 +282,7 @@ + diff --git a/Xamarin.Forms.Core.UnitTests/BehaviorTest.cs b/Xamarin.Forms.Core.UnitTests/BehaviorTest.cs index 902173890..464f97313 100644 --- a/Xamarin.Forms.Core.UnitTests/BehaviorTest.cs +++ b/Xamarin.Forms.Core.UnitTests/BehaviorTest.cs @@ -109,42 +109,5 @@ namespace Xamarin.Forms.Core.UnitTests collection.Remove (behavior); Assert.Null (behavior.AssociatedObject); } - - [Test] - //https://bugzilla.xamarin.com/show_bug.cgi?id=44074 - public void TestBehaviorsAreDetachedBeforeGarbageCollection() - { - WeakReference weakBindable = null; - - var attachCount = MockBehavior.AttachCount; - - int i = 0; - Action create = null; - create = () => - { - if (i++ < 1024) - { - create(); - return; - } - - var bindable = new MockBindable - { - Behaviors = { - new MockBehavior () - } - }; - weakBindable = new WeakReference(bindable); - }; - - create(); - - GC.Collect(); - GC.WaitForPendingFinalizers(); - GC.Collect(); - - Assert.False(weakBindable.IsAlive); - Assert.AreEqual(attachCount, MockBehavior.AttachCount); - } } } \ No newline at end of file diff --git a/Xamarin.Forms.Core/VisualElement.cs b/Xamarin.Forms.Core/VisualElement.cs index 8aba78ac3..520b06b17 100644 --- a/Xamarin.Forms.Core/VisualElement.cs +++ b/Xamarin.Forms.Core/VisualElement.cs @@ -796,18 +796,5 @@ namespace Xamarin.Forms public bool Result { get; set; } } - - ~VisualElement() - { - if (!GetIsDefault(BehaviorsProperty)) { - var behaviors = GetValue(BehaviorsProperty) as AttachedCollection; - behaviors.DetachFrom(this); - } - - if (!GetIsDefault(TriggersProperty)) { - var triggers = GetValue(TriggersProperty) as AttachedCollection; - triggers.DetachFrom(this); - } - } } } \ No newline at end of file diff --git a/docs/Xamarin.Forms.Core/Xamarin.Forms/VisualElement.xml b/docs/Xamarin.Forms.Core/Xamarin.Forms/VisualElement.xml index 73a1a4665..6f337aa26 100644 --- a/docs/Xamarin.Forms.Core/Xamarin.Forms/VisualElement.xml +++ b/docs/Xamarin.Forms.Core/Xamarin.Forms/VisualElement.xml @@ -352,22 +352,6 @@ To be added. - - - - Method - - 2.0.0.0 - - - System.Void - - - - Finalizer for visual elements. - To be added. - -