Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[android] fix memory leak in Window #16045

Merged
merged 3 commits into from
Jul 12, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Controls/src/Core/Window/Window.cs
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,7 @@ void IWindow.Destroying()
Destroying?.Invoke(this, EventArgs.Empty);
OnDestroying();

AlertManager.Unsubscribe();
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This call unsubscribes from MessagingCenter things on Android.

Application?.RemoveWindow(this);
Handler?.DisconnectHandler();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using AndroidX.AppCompat.App;
using AndroidX.Window.Layout;
using Microsoft.Maui.ApplicationModel;
Expand Down Expand Up @@ -55,14 +56,17 @@ static void OnConfigureLifeCycle(IAndroidLifecycleBuilder android)
})
.OnDestroy(activity =>
{
// If the activity is being recreated from a configuration change
// or something like the inspector getting attached then
// IsFinishing will be set to false so we still need to call
// Destroying to remove the xplat Window from Application
if (!activity.IsFinishing)
// There are cases IsFinishing was false in OnStop,
// and in OnDestroy we actually need to clean up the Window.
// In this case, it is possible to destroy the Window twice,
// so let's handle InvalidOperationException.
try
{
var window = activity.GetWindow();
window?.Destroying();
activity.GetWindow()?.Destroying();
}
catch (InvalidOperationException)
{
// if we somehow got "Window was already destroyed", ignore it
}
})
.OnBackPressed(activity =>
Expand Down