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

[net7.0] Memory Leak: MAUI-Windows are not garbage-collected after destroy of MainActivity #16240

Merged
merged 1 commit into from
Jul 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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/HandlerImpl/Window/Window.Impl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,7 @@ void IWindow.Destroying()
Destroying?.Invoke(this, EventArgs.Empty);
OnDestroying();

AlertManager.Unsubscribe();
Application?.RemoveWindow(this);
Handler?.DisconnectHandler();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,11 @@ internal void Subscribe(Window window)

internal void Unsubscribe(Window window)
{
IMauiContext mauiContext = window?.MauiContext;
IMauiContext mauiContext = window?.Handler?.MauiContext;
Context context = mauiContext?.Context;
Activity activity = context.GetActivity();
Activity activity = context?.GetActivity();
if (activity == null)
return;

var toRemove = Subscriptions.Where(s => s.Activity == activity).ToList();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,12 @@ internal void Subscribe(Window window)

internal void Unsubscribe(Window window)
{
var nativeWindow = window?.MauiContext.GetPlatformWindow();
IMauiContext mauiContext = window?.Handler?.MauiContext;
var platformWindow = mauiContext?.GetPlatformWindow();
if (platformWindow == null)
return;

var toRemove = Subscriptions.Where(s => s.Window == nativeWindow).ToList();
var toRemove = Subscriptions.Where(s => s.Window == platformWindow).ToList();

foreach (AlertRequestHelper alertRequestHelper in toRemove)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ internal void Subscribe(Window window)

internal void Unsubscribe(Window window)
{
var platformWindow = window.MauiContext.GetPlatformWindow();
IMauiContext? mauiContext = window?.Handler?.MauiContext;
var platformWindow = mauiContext?.GetPlatformWindow();
if (platformWindow == null)
return;

var toRemove = Subscriptions.Where(s => s.PlatformView == platformWindow).ToList();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ internal void Subscribe(Window window)

internal void Unsubscribe(Window window)
{
var platformWindow = window?.MauiContext.GetPlatformWindow();
IMauiContext mauiContext = window?.Handler?.MauiContext;
var platformWindow = mauiContext?.GetPlatformWindow();
if (platformWindow == null)
return;

var toRemove = Subscriptions.Where(s => s.PlatformView == platformWindow).ToList();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,8 @@ 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)
{
var window = activity.GetWindow();
window?.Destroying();
}
// If we tried to call window.Destroying() before, GetWindow() should return null
activity.GetWindow()?.Destroying();
})
.OnBackPressed(activity =>
{
Expand Down