Chris
Chris

Reputation: 357

Disposing object in wpf control

I am using an object that implements IDisposable in my WPF custom control. How can I ensure this object is disposed when the control is GC'ed? There's no Dispose() or any method in Control class that I can override to dispose my object.

Upvotes: 4

Views: 4222

Answers (4)

Chris
Chris

Reputation: 357

I think if you follow MVVM design pattern then resources should be held in the model or view model.

Plus, weak event pattern should be used to attach events.

Upvotes: 1

Michael T
Michael T

Reputation: 1367

I dealt with this last night. That is, I needed to call dispose on an object in a UserControl. I think you have two options.

I added a instance of BackgroundWorker to the WPF UserControl. That may or may not be a good idea, but whatever. Only one instance of the BackgroundWorker needs to be run per UserControl. So, I initialize the BackgroundWorker as null. When I need to use the BackgroundWorker, I check to see if it's null, and then create a new instance of it, have it do its work. Then, I dispose of it in the worker complete event, and set it back to null.

You can use the WPF Window closing or unload events. Unload doesn't get called on the UserControl, but it does in the Window that holds that UserControl.

WPFWindow Event Close (or Unload, or whatever) calls a custom release function in your UserControl. This custom release function does the work.

So, there's no real good one size fits all solution to your problem. It requires more design and thought to handle features that require dispose, because, in general, WPF doesn't require it. WPF generally only requires you to set items to null. (Does UserControl.Unload get called when you set the UserControl to null? Haven't thought about it before now, but if it does, that would be the correct answer.)

Upvotes: 0

Mark Hall
Mark Hall

Reputation: 54532

If you are using an object that needs to be disposed of, try using the Application.Shutdown Method to release the resources on shutdown, or try the Unloaded event to release the resources when you remove the control from the visual tree. You may need to use a combination of the two methods.

Try looking at this question to see if it helps

Upvotes: 1

Ed Swangren
Ed Swangren

Reputation: 124692

The finalizer should call Dispose if it has not been called already. If you are dealing with a .NET type then you can be assured that it will. The finalizer will get called when the object is GC'd and you'll be ok.

This is typical for controls such as a Picturebox that display a Bitmap. Unless you are swapping out that Bitmap often (in which case you should dispose of it) you can likely get by without being overly cautious.

That said, it comes down to your app and how it works. If you have a lot of objects that implement IDisposable that aren't needed anymore, but may have live references lying around for a relatively long time, then you should be more proactive and create a scheme to ensure these things are cleaned up as deterministically as possible.

Upvotes: 0

Related Questions