Hi,
I am currently developing an application in WPF platform and in that I have used the WPF-DatePicker to change date. I am facing memory leak by MS DatePicker’s automation peer, I have also tried to get rid of the memory leak using an FakeAutomationPeer in the application but to no success. Can anyone tell how to clear this leak?
MainWindow.xaml.cs:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
Button button = new Button()
{
HorizontalAlignment = HorizontalAlignment.Center,
VerticalAlignment = VerticalAlignment.Center,
Content = "Click"
};
button.Click += Button_Click;
this.Content = button;
}
private void Button_Click(object sender, RoutedEventArgs e)
{
var window = new Window1();
window.Show();
}
protected override AutomationPeer OnCreateAutomationPeer()
{
return new FakeWindowsPeer(this);
}
}
public class FakeWindowsPeer : WindowAutomationPeer
{
public FakeWindowsPeer(Window window)
: base(window)
{ }
protected override List<AutomationPeer> GetChildrenCore()
{
return null;
}
}
Window.xaml:
<Window
x:Class="DatePicker_MemeoryLeak.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:DatePicker_MemeoryLeak"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
Title="Window1"
Width="800"
Height="450"
Closing="Window_Closing"
mc:Ignorable="d">
<Grid>
<DatePicker
x:Name="DatePicker"
Width="200"
HorizontalAlignment="Center"
VerticalAlignment="Center" />
</Grid>
</Window>
Window.xaml.cs:
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
}
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
DatePicker = null;
GC.Collect();
}
}
Result Image :
Sample link : https://drive.google.com/file/d/1Cf4At8LKspQtXLKpqa841AUUbFltebWv/view?usp=sharing