From 56e3629c821248a3a6c547de901440055511092b Mon Sep 17 00:00:00 2001 From: adrianknight89 Date: Thu, 9 Feb 2017 10:03:41 -0600 Subject: [PATCH] [iOS] Fix NavigationPage memory leak when back button title is set (#523) * fix memory leak * fix parent * added comment --- .../Bugzilla44047.cs | 61 +++++++++++++++++++ ...rin.Forms.Controls.Issues.Shared.projitems | 1 + .../Renderers/NavigationRenderer.cs | 4 +- 3 files changed, 64 insertions(+), 2 deletions(-) create mode 100644 Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Bugzilla44047.cs diff --git a/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Bugzilla44047.cs b/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Bugzilla44047.cs new file mode 100644 index 000000000..a4d99357a --- /dev/null +++ b/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Bugzilla44047.cs @@ -0,0 +1,61 @@ +using System; +using Xamarin.Forms.CustomAttributes; +using Xamarin.Forms.Internals; + +#if UITEST +using Xamarin.UITest; +using NUnit.Framework; +#endif + +namespace Xamarin.Forms.Controls +{ + [Preserve(AllMembers = true)] + [Issue(IssueTracker.Bugzilla, 44047, "Memory leak when using SetBackButtonTitle on iOS", PlatformAffected.iOS)] + public class Bugzilla44047 : TestMasterDetailPage + { + protected override void Init() + { + Master = new ContentPage + { + Title = "Menu" + }; + + Detail = new NavigationPage(new Page1()); + } + } + + public class Page1 : ContentPage + { + public Page1() + { + Title = "Page1"; + Content = new Button + { + Text = "Open Page2", + Command = new Command(async o => + { + await (Parent as NavigationPage).PushAsync(new Page2()); + }) + }; + } + } + + public class Page2 : ContentPage + { + public Page2() + { + GC.Collect(); + GC.WaitForPendingFinalizers(); + GC.Collect(); + + Title = "Page2"; + System.Diagnostics.Debug.WriteLine("Constructor"); + NavigationPage.SetBackButtonTitle(this, "Custom"); + } + + ~Page2() + { + System.Diagnostics.Debug.WriteLine("Finalizer"); + } + } +} \ 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 e55302a95..742c29a7c 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 @@ -143,6 +143,7 @@ + diff --git a/Xamarin.Forms.Platform.iOS/Renderers/NavigationRenderer.cs b/Xamarin.Forms.Platform.iOS/Renderers/NavigationRenderer.cs index 1465b8dc9..e742fceee 100644 --- a/Xamarin.Forms.Platform.iOS/Renderers/NavigationRenderer.cs +++ b/Xamarin.Forms.Platform.iOS/Renderers/NavigationRenderer.cs @@ -366,8 +366,8 @@ namespace Xamarin.Forms.Platform.iOS var titleText = NavigationPage.GetBackButtonTitle(page); if (titleText != null) { - pack.NavigationItem.BackBarButtonItem = - new UIBarButtonItem(titleText, UIBarButtonItemStyle.Plain, async (o, e) => await PopViewAsync(page)); + // adding a custom event handler to UIBarButtonItem for navigating back seems to be ignored. + pack.NavigationItem.BackBarButtonItem = new UIBarButtonItem { Title = titleText, Style = UIBarButtonItemStyle.Plain }; } var pageRenderer = Platform.GetRenderer(page);