-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Labels
🚧 work in progressBugProduct bug (most likely)Product bug (most likely)regressionstatus: This issue is a regression from a previous build or releasestatus: This issue is a regression from a previous build or release
Description
Description
GC does not reclaim control memory in following scenario. The control is placed in the visual tree and then removed from it. Conditions must be met:
- PresentationTraceSources.Refresh() call
- there is a subscriber to the Unloaded event of control
Reproduction Steps
Run test TestLeak in project targeting net9.0-windows. NUnit version is 3.12.0.
using System;
using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Threading;
using NUnit.Framework;
namespace Sample {
[TestFixture]
public class MemoryTests {
[Test]
public void TestLeak() {
PresentationTraceSources.Refresh();
var window = new Window();
var wr = AddAndRemoveImage(window);
GC.Collect();
GC.WaitForPendingFinalizers();
Assert.IsFalse(wr.IsAlive);
}
static WeakReference AddAndRemoveImage(Window window) {
var img = new Image();
var wr = new WeakReference(img);
var holder = new ImageHolder(img);
window.Content = img;
window.Show();
DoEvents();
window.Content = null;
DoEvents();
return wr;
}
static void DoEvents() {
var frame = new DispatcherFrame();
Dispatcher.CurrentDispatcher.InvokeAsync(() => frame.Continue = false, DispatcherPriority.Background);
Dispatcher.PushFrame(frame);
}
class ImageHolder {
readonly Image image;
public ImageHolder(Image image) {
this.image = image;
this.image.Loaded += OnLoaded;
this.image.Unloaded += OnUnloaded;
}
void OnLoaded(object sender, RoutedEventArgs e) {
Debug.WriteLine("OnLoaded");
}
void OnUnloaded(object sender, RoutedEventArgs e) {
Debug.WriteLine("OnUnloaded");
}
}
}
}Expected behavior
The test passes.
Actual behavior
The test fails.
Regression?
This works in net8.0 and net462.
Known Workarounds
No response
Configuration
.NET: 9.0.0-preview.6.24327.6
OS: Windows 10, Version 22H2 (OS Build 19045.4651)
Architecture: x64
Other information
No response
Alexgoon and lindexi
Metadata
Metadata
Assignees
Labels
🚧 work in progressBugProduct bug (most likely)Product bug (most likely)regressionstatus: This issue is a regression from a previous build or releasestatus: This issue is a regression from a previous build or release