We have a class which derives from FrameworkElement and which is not under our control. This class registers eventhandlers in the OnInitialize method.
What is the pattern to properly clean this class up, since FrameworkElement does not provide a counterpart to OnInitialize?
It remains rooted since no part of it removes the EventHandler causing a leak.
There's no such thing as "deinitializing". Initialization methods are used when it isn't possible to fully initialize an object in its constructor, because it depends on data that isn't available during construction. In these cases construction is broken in two phases: the first step is executed in a parameterless constructor, the second in an initialization method, like
OnInitialize
, after the external data becomes availableWhat you describe is object disposal, which is performed by calling an object's
Dispose
method. A well-written class should clean up its data, release any external resources and release any event handlers.Visual elements typically have another step in their lifecycle, handled by the OnLoad/OnUnload methods. The Load step occurs when an element is actually placed in XAML view and connected to the other UI elements. Event handlers should be registered in the OnLoad method and removed in the OnUnload method.
If the element has no
Dispose
method , you may be able to raise the Unload event to force the cleanup, although this is a bit of a hack: