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

[windows] fix memory leak in MauiWinUIWindow #23327

Merged
merged 2 commits into from
Jul 4, 2024
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
25 changes: 25 additions & 0 deletions src/Controls/tests/DeviceTests/Memory/MemoryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,31 @@ await CreateHandlerAndAddToWindow<WindowHandlerStub>(new Window(page), async _ =
await AssertionExtensions.WaitForGC(references[0], references[1], references[2]);
}

[Fact("Window Does Not Leak")]
public async Task WindowDoesNotLeak()
{
SetupBuilder();

var references = new List<WeakReference>();

await InvokeOnMainThreadAsync(async () =>
{
var page = new ContentPage();
var window = new Window(page);
var handler = CreateHandler<WindowHandler>(window);
await OnLoadedAsync(page);
references.Add(new(window));
references.Add(new(window.Handler));
#if !WINDOWS
// FIXME: Microsoft.UI.Xaml.Window does not go away in this test
references.Add(new(window.Handler.PlatformView));
#endif
((IWindow)window).Destroying();
});

await AssertionExtensions.WaitForGC(references.ToArray());
}

#if IOS
[Fact]
public async Task ResignFirstResponderTouchGestureRecognizer()
Expand Down
2 changes: 1 addition & 1 deletion src/Core/src/Platform/Windows/MauiWinUIWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ void OnWindowMessage(object? sender, WindowMessageEventArgs e)
var rootManager = Window?.Handler?.MauiContext?.GetNavigationRootManager();
if (rootManager != null)
{
rootManager?.SetTitleBarVisibility(hasTitleBar);
rootManager?.SetTitleBarVisibility(this, hasTitleBar);
}
}
}
Expand Down
27 changes: 15 additions & 12 deletions src/Core/src/Platform/Windows/NavigationRootManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,26 @@ namespace Microsoft.Maui.Platform
{
public partial class NavigationRootManager
{
Window _platformWindow;
readonly WeakReference<Window> _platformWindow;
WindowRootView _rootView;
bool _disconnected = true;
internal event EventHandler? OnApplyTemplateFinished;

public NavigationRootManager(Window platformWindow)
{
_platformWindow = platformWindow;
_platformWindow = new(platformWindow);
_rootView = new WindowRootView();
_rootView.BackRequested += OnBackRequested;
_rootView.OnApplyTemplateFinished += WindowRootViewOnApplyTemplateFinished;

var titleBar = _platformWindow.GetAppWindow()?.TitleBar;
var titleBar = platformWindow.GetAppWindow()?.TitleBar;
if (titleBar is not null)
{
SetTitleBarVisibility(titleBar.ExtendsContentIntoTitleBar);
SetTitleBarVisibility(platformWindow, titleBar.ExtendsContentIntoTitleBar);
}
}

internal void SetTitleBarVisibility(bool isVisible)
internal void SetTitleBarVisibility(Window platformWindow, bool isVisible)
{
// https://learn.microsoft.com/en-us/windows/apps/design/basics/titlebar-design
// Standard title bar height is 32px
Expand All @@ -35,10 +35,10 @@ internal void SetTitleBarVisibility(bool isVisible)
var appbarHeight = isVisible ? 32 : 0;
if (isVisible && UI.Windowing.AppWindowTitleBar.IsCustomizationSupported())
{
var titleBar = _platformWindow.GetAppWindow()?.TitleBar;
var titleBar = platformWindow.GetAppWindow()?.TitleBar;
if (titleBar is not null)
{
var density = _platformWindow.GetDisplayDensity();
var density = platformWindow.GetDisplayDensity();
appbarHeight = (int)(titleBar.Height / density);
}
}
Expand All @@ -55,7 +55,7 @@ void WindowRootViewOnWindowTitleBarContentSizeChanged(object? sender, EventArgs
if (_disconnected)
return;

_platformWindow?
_platformWindow.GetTargetOrDefault()?
.GetWindow()?
.Handler?
.UpdateValue(nameof(IWindow.TitleBarDragRectangles));
Expand All @@ -66,7 +66,7 @@ void WindowRootViewOnApplyTemplateFinished(object? sender, System.EventArgs e) =

void OnBackRequested(NavigationView sender, NavigationViewBackRequestedEventArgs args)
{
_platformWindow
_platformWindow.GetTargetOrDefault()?
.GetWindow()?
.BackButtonClicked();
}
Expand Down Expand Up @@ -94,9 +94,9 @@ public virtual void Connect(UIElement platformView)
Content = platformView
};

if (_disconnected)
if (_disconnected && _platformWindow.TryGetTarget(out var platformWindow))
{
_platformWindow.Activated += OnWindowActivated;
platformWindow.Activated += OnWindowActivated;
_disconnected = false;
}

Expand All @@ -106,7 +106,10 @@ public virtual void Connect(UIElement platformView)
public virtual void Disconnect()
{
_rootView.OnWindowTitleBarContentSizeChanged -= WindowRootViewOnWindowTitleBarContentSizeChanged;
_platformWindow.Activated -= OnWindowActivated;
if (_platformWindow.TryGetTarget(out var platformWindow))
{
platformWindow.Activated -= OnWindowActivated;
}
SetToolbar(null);

if (_rootView.Content is RootNavigationView navView)
Expand Down
Loading