Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. WPF MVVM Dispose ViewModel

WPF MVVM Dispose ViewModel

Scheduled Pinned Locked Moved C#
wpfhelpcsharpdatabasearchitecture
5 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    Flowent57
    wrote on last edited by
    #1

    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 ?

    P B 2 Replies Last reply
    0
    • F Flowent57

      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 ?

      P Offline
      P Offline
      Pete OHanlon
      wrote on last edited by
      #2

      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

      D 1 Reply Last reply
      0
      • P Pete OHanlon

        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

        D Offline
        D Offline
        Daniel Grondal
        wrote on last edited by
        #3

        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.

        P 1 Reply Last reply
        0
        • D Daniel Grondal

          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.

          P Offline
          P Offline
          Pete OHanlon
          wrote on last edited by
          #4

          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

          1 Reply Last reply
          0
          • F Flowent57

            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 ?

            B Offline
            B Offline
            BobJanova
            wrote on last edited by
            #5

            There are two possible reasons for this:

            1. 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).
            2. 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?
            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups