locked
Converter Memory Leak RRS feed

  • Question

  • I have a converter that return a SolidColorBrush based on boolean value.

    After I performs a memory profiling on my tool, I found out that my view model was NOT GC'ed.

    Any idea how to solve this issue? 

    public class BoolToColorBrushesConverter2 : IValueConverter
        {
            public SolidColorBrush TrueColor { get; set; }
    
            public SolidColorBrush FalseColor { get; set; }
    
            public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            {
                if (value is bool)
                {
                    if ((bool)value)
                    {
                        return TrueColor;
                    }
                    else
                    {
                        return FalseColor;
                    }
                }
                else
                {
                    return null;
                }
                
            }
    
            public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            {
                throw new NotImplementedException();
            }
        }
    <lib:BoolToColorBrushesConverter2 x:Key="BoolToColorBrushesConverter2" TrueColor="Black" FalseColor="Gray"/>



    Thursday, April 23, 2015 8:50 AM

All replies

  • I don't really follow why you're showing code for a converter and complaining about a viewmodel.

    On the face of it, these would seem to be different things.

    What viewmodel, connected to what and when do you unload whatever it's connected to?


    Hope that helps.

    Technet articles: Uneventful MVVM; All my Technet Articles

    Thursday, April 23, 2015 12:48 PM
  • Basically my tool have a page that contains

    1) ComboBox

    • An ItemTemplate is being defined to change the Foreground color by using the the above-mentioned converter 
    <telerik:RadComboBox Width="300" SelectedItem="{Binding Path=SelectedSupplier, Mode=TwoWay}" 
                        CanAutocompleteSelectItems="False" CanKeyboardNavigationSelectItems="False" telerik:TextSearch.TextPath="SupplierName"                                                        
                        ItemsSource="{Binding Path=SelectableSuppliers, Mode=OneWay}" Margin="0,5,5,5"
                        VerticalAlignment="Top" 
                        EmptyText="select a supplier">
                        <telerik:RadComboBox.ItemTemplate>
                            <DataTemplate>
                                <TextBlock><Run Text="{Binding Path=SupplierName}" Foreground="{Binding Path=IsActive,Converter={StaticResource BoolToColorBrushesConverter2}, Mode=OneTime}"/></TextBlock>
                            </DataTemplate>
                        </telerik:RadComboBox.ItemTemplate>
     </telerik:RadComboBox>

    2) DataGrid

    • This grid will be filled "Tools" based on the selected supplier.

    The memory profiler (JustTrace) still showing "Tools" still in memory because of the BoolToColorBrushesConverter2 eventhough it is being referenced in the ItemTemplate.

    Hope this will make it clear.


    • Edited by ehor Thursday, April 23, 2015 1:53 PM
    Thursday, April 23, 2015 1:52 PM
  • Any feedback? 

    Tuesday, April 28, 2015 11:57 PM
  • I still don't follow the explanation.

    If you have some view which is connected to a viewmodel then both  will be in memory until you have no reference to either from active objects.

    Tools has something to do with that view ( I don't follow how exactly ) and it'll still be hanging around in memory whilst you have a reference to it from the view.

    Or vice versa.

    As to the converter.

    It's referenced and will be in memory whilst your view is in memory.

    Or anything else that uses it.

    By default you get one single instance of a converter for an application. 

    I suspect JustTrace is giving you a misleading result. 


    Hope that helps.

    Technet articles: Uneventful MVVM; All my Technet Articles

    Wednesday, April 29, 2015 2:25 PM