Skip to content

Commit

Permalink
Fix Memory leak on Popup (#1398)
Browse files Browse the repository at this point in the history
* Null the parent to avoid leaks

* Remove duplicate code

Both `CloseAsync(object)` and `OnDismissedByTappingOutsideOfPopup()` `await` the `OnClosed(object, bool)` method.

We can just set the Parent reference to `null` in `OnClosed(object,bool)` and remove the now unused field.

* `dotnet format`

---------

Co-authored-by: Brandon Minnick <13558917+brminnick@users.noreply.github.com>
  • Loading branch information
pictos and brminnick committed Sep 23, 2023
1 parent bf9d66d commit 322547e
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
Expand Up @@ -95,7 +95,11 @@ public static void MapSize(PopupHandler handler, IPopup view)
/// <inheritdoc/>
protected override void DisconnectHandler(Popup platformView)
{
ArgumentNullException.ThrowIfNull(VirtualView.Parent);
if (VirtualView.Parent is null)
{
return;
}

ArgumentNullException.ThrowIfNull(VirtualView.Handler?.MauiContext);
var parent = VirtualView.Parent.ToPlatform(VirtualView.Handler.MauiContext);
parent.IsHitTestVisible = true;
Expand Down
1 change: 1 addition & 0 deletions src/CommunityToolkit.Maui/Views/Popup/Popup.shared.cs
Expand Up @@ -250,6 +250,7 @@ protected virtual async Task OnClosed(object? result, bool wasDismissedByTapping
((IPopup)this).OnClosed(result);

await popupDismissedTaskCompletionSource.Task;
Parent = null;

dismissWeakEventManager.HandleEvent(this, new PopupClosedEventArgs(result, wasDismissedByTappingOutsideOfPopup), nameof(Closed));
}
Expand Down

0 comments on commit 322547e

Please sign in to comment.