From 753616a93a2760fdb7756148ecf8068bd1251e05 Mon Sep 17 00:00:00 2001 From: Carl de Billy Date: Thu, 15 Oct 2020 20:22:17 -0400 Subject: [PATCH] fix(commandbar): Removed potential memory leak --- .../Controls/NativeCommandBarPresenter.iOS.cs | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/src/Uno.UI/Controls/NativeCommandBarPresenter.iOS.cs b/src/Uno.UI/Controls/NativeCommandBarPresenter.iOS.cs index 11d0f6761553..b231ec9a7249 100644 --- a/src/Uno.UI/Controls/NativeCommandBarPresenter.iOS.cs +++ b/src/Uno.UI/Controls/NativeCommandBarPresenter.iOS.cs @@ -22,7 +22,7 @@ public partial class NativeCommandBarPresenter : ContentPresenter { private readonly SerialDisposable _statusBarSubscription = new SerialDisposable(); private readonly SerialDisposable _orientationSubscription = new SerialDisposable(); - private CommandBar? _commandBar; + private WeakReference? _commandBar; private UINavigationBar? _navigationBar; private readonly bool _isPhone = UIDevice.CurrentDevice.UserInterfaceIdiom == UIUserInterfaceIdiom.Phone; @@ -33,14 +33,19 @@ private protected override void OnLoaded() // TODO: Find a proper way to decide whether a CommandBar exists on canvas (within Page), or is mapped to the UINavigationController's NavigationBar. - if (_commandBar == null) + var commandBar = _commandBar?.Target; + + if (commandBar == null) { - _commandBar = TemplatedParent as CommandBar; - _navigationBar = _commandBar?.GetRenderer(RendererFactory).Native; + commandBar = TemplatedParent as CommandBar; + _commandBar = new WeakReference(commandBar); + + _navigationBar = commandBar?.GetRenderer(RendererFactory).Native; + } else { - _navigationBar = _commandBar?.ResetRenderer(RendererFactory).Native; + _navigationBar = commandBar?.ResetRenderer(RendererFactory).Native; } if (_navigationBar == null) @@ -75,7 +80,7 @@ void OnStatusBarChanged(StatusBar sender, object args) } } - CommandBarRenderer RendererFactory() => new CommandBarRenderer(_commandBar!); + CommandBarRenderer RendererFactory() => new CommandBarRenderer(_commandBar?.Target); protected override Size MeasureOverride(Size size) {