I have the following line of code in a usercontrol:
Application.Current.RootVisual.MouseLeftButtonDown += RootClicked;
When it’s defined the parent usercontrol is no longer collected by the GC or destroyed, the RootClicked event keeps firing even after it should no longer be active.
if I add:
Application.Current.RootVisual.MouseLeftButtonDown -= RootClicked;
The issue is no longer present.
Any ideas why this is? Is there a function I should explicitly remove the event?
This is a normal behaviour and a common source of memory leaks in .NET.
When you attach an instance method of object A to an event of object B, you are adding in the list of event receivers the instance of A, so the garbage collector cannot free up the object A until you remove the handler or you release every reference to both object A and B.
This is an expected and normal behaviour.
There are two common ways to solve the problem:
1) You unsubscribe object A from the event when you don’t need the object A anymore.
2) You use weak events, like WPF does.
Weak events are usually implemented with the class WeakReference, you can find more informations on the web.