3

Each time I open a view the number of get requests to each of the model properties increment by one. Just to be clear if I open the view once, close it and open it again there are two get requests to each property, and if the property is set and RaisePropertyChanged("propertName") is triggered there are two get requests again. The number of get requests is always equal to the number of times the view has been instantiated! I have debugged the life out of this issue and everything points towards it being that the user controls aren't being disposed of when the view is closed and the bindings are withheld.

What should I do to deal with this? I have looked into disposal of user controls however I have been unsuccessful in finding something to help me fix this. Maybe I am not looking for the right topic? Any help or pointers would be greatly appreciated - thanks!

Sam
  • 586
  • 1
  • 6
  • 25
  • We need to know more details about your view and your view model. – Alberto Oct 07 '13 at 10:34
  • @Alberto - sorry, what specifically would help? I can of course post all of it if you would prefer? Thinking about it, my view is a CustomDialog (which is basically a custom window) - could the problem be there? That any children aren't disposed of when the window is closed? – Sam Oct 07 '13 at 10:37
  • Are you actually **closing** the window or just hiding (setting Visibility to Hidden) it? – Suresh Oct 07 '13 at 10:39
  • @sthotakura The window is closed, not hidden :/ – Sam Oct 07 '13 at 10:41

1 Answers1

5

WPF controls do not implement IDisposable and, hence, don't need to be disposed of. I believe that you want to say "is not being marked as eligible to garbage collection"

it seems that your problem is related to what is called an event reference. Some of your living instance (some class you have which is not your window/user control) retains a reference to an event. If you close the Window or UserControl that link still lives on and it is not cleared automatically.

When closing the window/user control you should dereference your event like this

EventName-= methodHandler or this.UserControlInstance=null

You may read some interesting pattern here

Luis Filipe
  • 8,488
  • 7
  • 48
  • 76
  • Okay, I get what you are saying, however I am unsure at which point to do this! Of course I could hack this together probably get it to seem to be working, however I would like to implement it properly to ensure everything is cleaned up correctly behind the scenes! So, what I am thinking is to attach it to the base window closing event and set any user control instance to null from there? Or should I look to do this in some kind of unloaded event method in the user control its self? – Sam Oct 07 '13 at 10:58
  • Hooking it in a base class closing event seems the right thing to do. Alternatively if you have a "CloseControl()" method you can do the clean up there too. – Luis Filipe Oct 07 '13 at 11:23
  • 1
    The issue was fixed by using _BindingOperations.ClearAllBindings()_ and attaching it to the windows closing event, BUT what I find incredible is that this issue was not limited to my UserControls, it was also an issue for the standard WPF controls, the bindings simply weren't being dropped when the window is closed! Surely this is a pretty serious bug? I am happy to post any code (for anyone reading this) as I am pretty sure it is not an issue with the way I am binding as I am doing it the standard (nothing special) way. Accepting this answer for pointing me in the right direction - thanks. – Sam Oct 07 '13 at 11:45
  • 1
    Sorry, one more thing Luis, whilst this all "seems" to be working fine now, is there a way I can actually determine whether or not this is fixing the problem or just covering it up? As the controls may still exist and retain there end of the binding, but as it has been detached from the window there is no way of actually knowing whether or not that is the case... does that make sense? – Sam Oct 07 '13 at 11:48
  • @sam i currently don't use WPF and i've never used the BindingOperations.ClearAllBindings(). I used the -= to unassign events. I'd say, without being completely sure, that if your events stopped firing twice everything is fine. – Luis Filipe Oct 07 '13 at 15:33