While using converter as static resource in custom control Memory leak occurs even when the window is closed.
Sample attached and please find the screenshot below.
Can you please suggest any solution for this?
Note: I have checked this in ANTS profile.
Sample code to create custom control. This control used in simple sample.
Memory leak replication steps with sample description:
Use the created custom control in simple sample.
Before and after closing the window, (which window have the created custom control) check memory leak in ANTS profile.
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:customcontrol">
<local:YesNoToBooleanConverter x:Key="YesNoToBooleanConverter"/>
<Style TargetType="{x:Type local:CustomControl1}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:CustomControl1}">
<Border Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}">
<StackPanel Orientation="Horizontal">
<TextBox Name="txtValue" Height="50" Width="100" />
<CheckBox IsChecked="{Binding ElementName=txtValue,
Path=Text,
Converter={StaticResource YesNoToBooleanConverter}}" />
</StackPanel>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
public class CustomControl1 : Control
{
static CustomControl1()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(CustomControl1), new FrameworkPropertyMetadata(typeof(CustomControl1)));
}
}
public class YesNoToBooleanConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter,
System.Globalization.CultureInfo culture)
{
switch (value.ToString().ToLower())
{
case "yes":
return true;
case "no":
return false;
default:
return Binding.DoNothing;
}
}
public object ConvertBack(object value, Type targetType, object parameter,
System.Globalization.CultureInfo culture)
{
if (value is bool)
{
if ((bool)value == true)
return "yes";
else
return "no";
}
return "no";
}
}
Regards, Priyanga B