views:

356

answers:

3

I have a WinForms TabControl that I am dynamically adding TabPages to at runtime. Each TabPage contains a WebBrowser control. I also have the ability to remove the TabPages at runtime.

Should I bother Dispose()ing the TabPage and/or WebBrowser controls?

It seems to me I should at least Dispose() the WebBrowser control since it is a bit of a resource hog.

+2  A: 

Everything you allocate that implements IDisposable should have Dispose called on it. That's the purpose of implementing IDisposable.

John Saunders
+1  A: 

If you explicitly call Dispose(), it will generally be cleaned up faster than if you don't. If you are concerned about resources, or your objects are holding onto other resources that might be scarce or in high demand, it's always a good idea to call Dispose() explicitly.

I always recommend this CodeProject article to help people understand the Dispose pattern properly, and what Dispose is all about. http://www.codeproject.com/KB/cs/idisposable.aspx

womp
Garbage collection is not the same as calling Dispose. Dispose is not guaranteed to be called, ever, and it is usually implemented because a class is using some resource that the GC will not collect, so it needs to be called explicitly no matter what.
Ed Swangren
@Ed: That is only somewhat true. If you follow the design guidelines correctly, any object that requires a dispose to release unmanaged resources should also provide a finalizer that will do this when the GC finalizes the object. This requires trusting the component writer to do things correctly, though. For details, see my blog entries on IDisposable: http://reedcopsey.com/?p=5 I go into this in a lot of detail.
Reed Copsey
That is true, I forgot about Finalizers :-)
Ed Swangren
Thanks for that, removed my -1
Ed Swangren
I was assuming that no unmanaged resources were in play. I tend to approach Dispose() gently as there is a lot of misunderstanding of it out there. My post was assuming explicit cleanup of managed resources, which calling Dispose() on simply gives you a deterministic way of handling. I don't necessarily agree that it's usually implemented to deal with unmanaged resources, it is often implemented to give the consumer a single point of call to clean up multiple or large resources that may have scarcity issues as well.
womp
@womp: IDisposable was originally designed specifically for unmanaged resources. Managed "resources" will be cleaned just by setting to null. It can also be used for handling factored types (and often is in the BCL), but that is a different purpose. In this case, Controls in Windows Forms use unmanaged resources (window handles), so disposing is critical.
Reed Copsey
@Reed - ah, I didn't realize that about every control on winforms, which makes the assumption behind my post more than a bit idiotic. That's my asp.net background shining through. I do, however, totally understand the purpose and proper usage of IDisposable for both types of resources.
womp
@womp - that is an excellent article on IDisposable - very readable.Thanks for that.
+1  A: 

You should Dispose() your tab page when you remove it. This will automatically dispose all of the child controls.

For details, see the Control.Dispose documentation:

Releases the unmanaged resources used by the Control and its child controls and optionally releases the managed resources.

The tab page's dispose will also dispose of all of the children controls for you.

Reed Copsey
I went with a simple bit of code like this...webBrowserTabControl.TabPages.Remove(tabPage);tabPage.Dispose();Works well and looks like both the TabPage and WebBrowser control are freed up nicely.Thanks!!