Reputation: 10509
My question is about memory handling in WPF. I have created a user control with notрing but XAML witch contains only standard WPF controls. I didn't implement any code in code-behind at this point.
In my application, I create an instance of this user control to display to a user. When I no longer need the user control, I can just assign it's reference variable a null value.
Do I need to write a .Dispose method myself and dispose inner controls in my user control? Or is it a good practice to leave this job to a garbage collector? Thanks.
Upvotes: 3
Views: 1600
Reputation: 564451
Typically, in WPF, you don't need to make your controls IDisposable
. Unlike Windows Forms, WPF UIElement
objects are completely managed, and not (normally) wrapping native handles. As such, they don't need to be disposed, and can be left to the garbage collector.
This is why UserControl
does not implement IDisposable
.
There are, of course, exceptions. If your class encapsulates anything deriving from HwndHost
(such as WebBrowser), for example, you will likely want to make your class IDisposable
in order to call Dispose()
on the encapsulated control. This is normally only required in interop scenarios (ie: WebBrowser
, which interops with the native browser controls).
Upvotes: 7