-
-
Notifications
You must be signed in to change notification settings - Fork 943
Open
Labels
⭐ top bugTop bug.Top bug.⭐ top issueTop issue.Top issue.bugSomething isn't workingSomething isn't working
Description
Describe the bug
A memory leak occurs in FluentWindow. The window instance and its DataContext are not collected by the GC because an anonymous delegate is subscribed to the static event ApplicationThemeManager.Changed without being unsubscribed. This results in a continuous increase in the EventHandler count.
To Reproduce
- Create a Window class inheriting from
FluentWindowand add a finalizer with aDebug.WriteLineto track its destruction:
public class TestWindow : FluentWindow
{
~TestWindow()
{
Debug.WriteLine("Destruction");
}
}- Open and close the
TestWindowmultiple times. - Perform a manual Garbage Collection (GC) and check the Output Window.
- The finalizer is never called (the "Destruction" log does not appear), and the
EventHandlercount increases continuously in the memory profiler.
Expected behavior
When the window is closed, all event subscriptions—especially those to static events—should be released so the FluentWindow and its ViewModel can be garbage collected.
Screenshots
No response
OS version
Windows 11 25H2 26200.7462
.NET version
.NET9
WPF-UI NuGet version
4.20
Additional context
I have implemented a temporary workaround for this issue as follows
protected override void OnClosed(EventArgs e)
{
base.OnClosed(e);
FixBug();
}private void FixBug()
{
Type? type = typeof(ApplicationThemeManager);
FieldInfo? eventField = type.GetField("Changed", BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Instance);
if (eventField != null)
{
MulticastDelegate? eventDelegate = eventField.GetValue(null) as MulticastDelegate;
if (eventDelegate != null)
{
Delegate[] invocationList = eventDelegate.GetInvocationList();
foreach (Delegate handler in invocationList)
{
if (handler.Target == this)
{
ApplicationThemeManager.Changed -= (ThemeChangedEvent)handler;
}
}
}
}
}Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
⭐ top bugTop bug.Top bug.⭐ top issueTop issue.Top issue.bugSomething isn't workingSomething isn't working