I have a third party ActiveX control that I am embedding in a form. The control's class implements IDisposable. My question is should I manually call the control's dispose method either in the form closing event or go into the designer and edit the form dispose method to call the control's dispose.
I understand that a form calls the dispose methods of all the controls it owns, however are there any exceptions since this is a COM control. Thanks.
PS: I am using Visual Studio 2008, .Net 3.5, Project Type: Windows Forms ApplicationAnswer1:
No, there is nothing special you have to do.
Because the <a href="http://msdn.microsoft.com/en-us/library/system.windows.forms.control.aspx" rel="nofollow">
Control class</a> (from which other container controls such as <a href="http://msdn.microsoft.com/en-us/library/system.windows.forms.form.aspx" rel="nofollow">
Form</a> inherit from) implements the <a href="http://msdn.microsoft.com/en-us/library/system.idisposable.aspx" rel="nofollow">
IDisposable interface</a>, it should (if it follows the standard <a href="http://msdn.microsoft.com/en-us/library/b1yfkh5e.aspx" rel="nofollow">
IDisposable implementation guidelines</a>) dispose of any references that it holds that implement the
That said, your control, assuming that it is in the controls collection exposed by the <a href="http://msdn.microsoft.com/en-us/library/system.windows.forms.control.controls.aspx" rel="nofollow">
Controls property</a>, will have it's <a href="http://msdn.microsoft.com/en-us/library/system.idisposable.dispose.aspx" rel="nofollow">
IDisposable.Dispose method</a> called when the window containing it is closed (as closing the window causes the window's
Dispose method to be called).
<a href="http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.marshal.releasecomobject.aspx" rel="nofollow">MSDN Marshal.ReleaseComObject</a> In order to Release or Free a COM Object you need to use this code
//this is your COM Object Dispose will not work on a COM Object for example Debug and try to Dispose your object using code like this if you have an object named fs