WPF MVVM Dispose ViewModel
-
Hi, I have a treeview with one root node. This node has some children. The root not is a "TreeViewItemViewModel". This class contains : "ObservableCollection<TreeViewItemViewModel> Items" which are Children nodes. At beginning, the root node is not expanded and "Children" is empty. When I expand root node, I load data from DataBase in Children collection :
public override void LoadChildren()
{
this.Items.Clear();
List lstOp = DataBAse.GetAllOperations();
foreach (Operation op in lstOp)
{
this.Items.Add(new TreeViewItemViewModel(op));
}
}If I "unexpand" and expand the root node, I reload children...
public bool IsExpanded
{
get
{
return _isExpanded;
}
set
{
if (value != this._isExpanded)
{
this._isExpanded = value;
RaisePropertyChanged("IsExpanded");if (this.\_isExpanded) { this.LoadChildren(); } } } }
My issue : when I call "LoadChildren", Items list is cleared but viewmodel are always in memory... Can someone help me ?
-
Hi, I have a treeview with one root node. This node has some children. The root not is a "TreeViewItemViewModel". This class contains : "ObservableCollection<TreeViewItemViewModel> Items" which are Children nodes. At beginning, the root node is not expanded and "Children" is empty. When I expand root node, I load data from DataBase in Children collection :
public override void LoadChildren()
{
this.Items.Clear();
List lstOp = DataBAse.GetAllOperations();
foreach (Operation op in lstOp)
{
this.Items.Add(new TreeViewItemViewModel(op));
}
}If I "unexpand" and expand the root node, I reload children...
public bool IsExpanded
{
get
{
return _isExpanded;
}
set
{
if (value != this._isExpanded)
{
this._isExpanded = value;
RaisePropertyChanged("IsExpanded");if (this.\_isExpanded) { this.LoadChildren(); } } } }
My issue : when I call "LoadChildren", Items list is cleared but viewmodel are always in memory... Can someone help me ?
Of course the VM is in memory. It's the active object - that's what the
this.
reference indicates - it's the current instance. I'm not sure what you are expecting here.Forgive your enemies - it messes with their heads
"Mind bleach! Send me mind bleach!" - Nagy Vilmos
My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility
-
Of course the VM is in memory. It's the active object - that's what the
this.
reference indicates - it's the current instance. I'm not sure what you are expecting here.Forgive your enemies - it messes with their heads
"Mind bleach! Send me mind bleach!" - Nagy Vilmos
My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility
My guess is that he refers to the TreeviewItemViewModels he adds to this.Items. That said, I still do not understand what he actually means with they being in memory. They will be garbage collected by the GC when no more references exists and when a the GC is triggered.
-
My guess is that he refers to the TreeviewItemViewModels he adds to this.Items. That said, I still do not understand what he actually means with they being in memory. They will be garbage collected by the GC when no more references exists and when a the GC is triggered.
Gotcha. It was the singular of ViewModel that threw me there.
Forgive your enemies - it messes with their heads
"Mind bleach! Send me mind bleach!" - Nagy Vilmos
My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility
-
Hi, I have a treeview with one root node. This node has some children. The root not is a "TreeViewItemViewModel". This class contains : "ObservableCollection<TreeViewItemViewModel> Items" which are Children nodes. At beginning, the root node is not expanded and "Children" is empty. When I expand root node, I load data from DataBase in Children collection :
public override void LoadChildren()
{
this.Items.Clear();
List lstOp = DataBAse.GetAllOperations();
foreach (Operation op in lstOp)
{
this.Items.Add(new TreeViewItemViewModel(op));
}
}If I "unexpand" and expand the root node, I reload children...
public bool IsExpanded
{
get
{
return _isExpanded;
}
set
{
if (value != this._isExpanded)
{
this._isExpanded = value;
RaisePropertyChanged("IsExpanded");if (this.\_isExpanded) { this.LoadChildren(); } } } }
My issue : when I call "LoadChildren", Items list is cleared but viewmodel are always in memory... Can someone help me ?
There are two possible reasons for this:
- They have been dereferenced, but the GC hasn't got around to picking them up yet. If your machine is not running short on memory, the .Net GC is very relaxed about bothering to reclaim unused memory. There's not much you can do about this, but you can put a button on your form that calls GC.Collect for memory checking purposes only and see if the view models get collected (you can give them a destructor that echoes to the console, again for debugging only, to find out).
- You're saving a reference to those view models somewhere. The obvious place is the tree view itself – are you putting a reference to the view model (or to a view class which is tied to the view model) into the Tag property of tree nodes which you never clear? Is the database loading process attaching event handlers which keep references to the view models alive?