Skip to content

Commit

Permalink
Clear native menu items after closing window to prevent memory leakage.
Browse files Browse the repository at this point in the history
  • Loading branch information
hamster620 committed Feb 2, 2024
1 parent 344354f commit f29ee6d
Showing 1 changed file with 38 additions and 1 deletion.
39 changes: 38 additions & 1 deletion PixelViewer/MainWindow.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,21 @@ static void DetachTabItemFromSession(TabItem tabItem)
(tabItem.Content as Control)?.Let((it) => it.DataContext = null);
tabItem.DataContext = null;
}


// Dispose native menu item properly.
void DisposeNativeMenuItem(NativeMenuItem menuItem)
{
menuItem.Menu?.Let(menu =>
{
foreach (var item in menu.Items)
{
if (item is NativeMenuItem menuItem)
this.DisposeNativeMenuItem(menuItem);
}
menu.Items.Clear();
});
}


// Find index of main tab item attached to given session.
Expand Down Expand Up @@ -345,7 +360,29 @@ protected override void OnAttachToViewModel(Workspace workspace)
}


// Detach from view-model.
/// <inheritdoc/>
protected override void OnClosed(EventArgs e)
{
// [Workaround] Remove bindings to window to prevent window leakage
if (Platform.IsMacOS)
{
NativeMenu.GetMenu(this)?.Let(menu =>
{
foreach (var item in menu.Items)
{
if (item is NativeMenuItem menuItem)
this.DisposeNativeMenuItem(menuItem);
}
menu.Items.Clear();
});
}

// call base
base.OnClosed(e);
}


// Detach from view-model.
protected override void OnDetachFromViewModel(Workspace workspace)
{
// detach from activated session
Expand Down

0 comments on commit f29ee6d

Please sign in to comment.