I am creating a WPF application that connects to a SQL Server database using LINQ to SQL.
The main application window contains a ListView containing a series of detailed views. The ItemSource ListView bound to a set of detailed view model objects displayed as a property in the root view model. Each detailed view model object contains several ICommand properties, as well as a property representing the detailed model object, which in turn provides various data fields displayed in the user interface.
Analysis with the ANTS memory profiler shows that leaked objects are those contained in the object of the detailed model and some user interface classes to which they are attached. Instances of these objects from previous updates are not garbage collected.
ANTS has a tool that allows the user to track link chains to determine why unwanted memory is stored. When I use it, I find that all the chains that appear have ICommand in them. Accordingly, I removed the ICommand insult and found that the memory leak disappeared.
Unfortunately, I need ICommand implement some important functions. I am really confused about how it refers to the part model object in the first place - these are two completely separate instance variables in the detailed view model object.
Here is the constructor of the detail view model object (the RootViewModel link is used for callbacks in some methods related to ICommands. Initially, I suspected that this might cause a circular chain of links, which might be the cause of the problem, but deleting it seems to have no effect .)
public CarDataViewModel(CarData carDataItem, RootViewModel parentViewModel) { _parentViewModel = parentViewModel; CarDataModel = carDataItem; CompetingCheckboxStatus = CarDataModel.CurrentCar.Competing; AcknowledgeAlarm = new ParameterlessCommand(AcknowledgeAlarmClicked); Acknowledge = new ParameterlessCommand(AcknowledgeClicked); ShowReport = new ParameterlessCommand(ShowReportClicked); Cancel = new ParameterlessCommand(CancelClicked); }
Here xaml where the bindings are configured - AcknowledgeAlarm is ICommand, CarDataModel is the part model object:
<ListView x:Name="itemGridView"Grid.Row="1"ScrollViewer.HorizontalScrollBarVisibility="Disabled" ItemsSource="{Binding CarDataViewModels}" IsSynchronizedWithCurrentItem="True" Margin="0,0,0,0"> <ListView.ItemTemplate> <DataTemplate> </DataTemplate.Resources> <Button Command="{Binding AcknowledgeAlarm}"> <Border DataContext="{Binding CarDataModel}" BorderBrush="{StaticResource GrayFadeBrush}" Background="White" BorderThickness="5"> <Grid> . . .
Rich tolley
source share