Fix NullReferenceException in VisualElement finalize. (#701)
authorKevin Petit <kevin.petit@outlook.com>
Thu, 19 Jan 2017 09:12:51 +0000 (10:12 +0100)
committerJason Smith <jason.smith@xamarin.com>
Thu, 19 Jan 2017 19:40:36 +0000 (11:40 -0800)
Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Bugzilla51503.cs [new file with mode: 0644]
Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Xamarin.Forms.Controls.Issues.Shared.projitems
Xamarin.Forms.Core/VisualElement.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 (file)
index 0000000..e9cdebc
--- /dev/null
@@ -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
index e040a04..ba7de6c 100644 (file)
     <Compile Include="$(MSBuildThisFileDirectory)Bugzilla28650.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)Bugzilla37431.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)Bugzilla44777.cs" />
+    <Compile Include="$(MSBuildThisFileDirectory)Bugzilla51503.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)_Template.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)Issue1028.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)Issue1075.cs" />
index cff1eb1..352e254 100644 (file)
@@ -790,7 +790,7 @@ namespace Xamarin.Forms
                        }
 
                        if (!GetIsDefault(TriggersProperty)) {
-                               var triggers = GetValue(TriggersProperty) as AttachedCollection<Trigger>;
+                               var triggers = GetValue(TriggersProperty) as AttachedCollection<TriggerBase>;
                                triggers.DetachFrom(this);
                        }
                }