Portikus
Portikus

Reputation: 327

How to deinitialize a FrameworkElement?

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.

Upvotes: 3

Views: 2545

Answers (3)

Cluster
Cluster

Reputation: 1118

Use Dispatcher.ShutdownStarted event.

Add this to the constructor:

Dispatcher.ShutdownStarted += Dispatcher_ShutdownStarted;

And add your cleanup code to this method:

private void Dispatcher_ShutdownStarted(object sender, EventArgs e)
{
    Stop();
}

Upvotes: 0

Panagiotis Kanavos
Panagiotis Kanavos

Reputation: 131324

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 available

What 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:

control.RaiseEvent(new RoutedEventArgs(FrameworkElement.UloadedEvent))

Upvotes: 3

Dennis
Dennis

Reputation: 37770

Use weak event pattern. In particular, subscribe to events using WeakEventManager implementations (or, better, its generic version WeakEventManager<TEventSource, TEventArgs>, if .NET 4.5 is an option).

Upvotes: 2

Related Questions