From a48f2d28e4e70985714d7dc7ff1c37d3ee536e79 Mon Sep 17 00:00:00 2001 From: Kevin Petit Date: Thu, 19 Jan 2017 10:12:51 +0100 Subject: [PATCH] Fix NullReferenceException in VisualElement finalize. (#701) --- .../Bugzilla51503.cs | 80 ++++++++++++++++++++++ .../Xamarin.Forms.Controls.Issues.Shared.projitems | 1 + Xamarin.Forms.Core/VisualElement.cs | 2 +- 3 files changed, 82 insertions(+), 1 deletion(-) create mode 100644 Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Bugzilla51503.cs diff --git a/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Bugzilla51503.cs b/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Bugzilla51503.cs new file mode 100644 index 0000000..e9cdebc --- /dev/null +++ b/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Bugzilla51503.cs @@ -0,0 +1,80 @@ +using System; +using Xamarin.Forms.CustomAttributes; +using Xamarin.Forms.Internals; + +#if UITEST +using Xamarin.UITest; +using NUnit.Framework; +#endif + +namespace Xamarin.Forms.Controls.Issues +{ + [Preserve(AllMembers = true)] + [Issue(IssueTracker.Bugzilla, 51503, "NullReferenceException on VisualElement Finalize", PlatformAffected.All)] + public class Bugzilla51503 : TestNavigationPage + { + protected override void Init() + { + PushAsync(new RootPage()); + } + + [Preserve(AllMembers = true)] + class RootPage : ContentPage + { + public RootPage() + { + Button button = new Button + { + AutomationId = "Button", + Text = "Open" + }; + + button.Clicked += Button_Clicked; + + Content = button; + } + + async void Button_Clicked(object sender, EventArgs e) + { + GC.Collect(); + GC.WaitForPendingFinalizers(); + + await Navigation.PushAsync(new ChildPage()); + } + } + + [Preserve(AllMembers = true)] + class ChildPage : ContentPage + { + public ChildPage() + { + Content = new Label + { + AutomationId = "VisualElement", + Text = "Navigate 3 times to this page", + Triggers = + { + new EventTrigger() + } + }; + } + } + +#if UITEST +[Test] + public void Issue51503Test() + { + for (int i = 0; i < 3; i++) + { + RunningApp.WaitForElement(q => q.Marked("Button")); + + RunningApp.Tap(q => q.Marked("Button")); + + RunningApp.WaitForElement(q => q.Marked("VisualElement")); + + RunningApp.Back(); + } + } +#endif + } +} \ 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 e040a04..ba7de6c 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 @@ -222,6 +222,7 @@ + diff --git a/Xamarin.Forms.Core/VisualElement.cs b/Xamarin.Forms.Core/VisualElement.cs index cff1eb1..352e254 100644 --- a/Xamarin.Forms.Core/VisualElement.cs +++ b/Xamarin.Forms.Core/VisualElement.cs @@ -790,7 +790,7 @@ namespace Xamarin.Forms } if (!GetIsDefault(TriggersProperty)) { - var triggers = GetValue(TriggersProperty) as AttachedCollection; + var triggers = GetValue(TriggersProperty) as AttachedCollection; triggers.DetachFrom(this); } } -- 2.7.4