3
votes

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.

3

3 Answers

2
votes

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.

2
votes

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

1
votes

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