73919

Disposing ActiveX Control on a Form

Question:

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 Application

Answer1:

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 IDisposable interface.

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).

Answer2:

<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

System.Runtime.InteropServices.Marshal.ReleaseComObject( someComObject);

//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

((IDisposable)fs).Dispose();

Recommend

  • How to deal with concurrency issues brought by NSStream run loop scheduling using GCD?
  • JFrame attached on the side of another JFrame
  • Development workflow for server and client using Docker Compose?
  • Accessing Windows Azure Queues from client side javascript/jquery
  • DotNetOpenAuth - how to uniquely identify Google users?
  • not able to create VC++ project, with VS11
  • How to format code on aptana 3?
  • Ability to use Bootstrap 3 grid system to specify width of select element
  • How can I emulate a recursive type definition in C++?
  • Visual studio 2015 keystroke with mouse button
  • Creating a C++ function that calls other Lua function
  • Upgrade project from WorkLight 6.1 to MobileFirst 7.1
  • How to enable large page memory for the JVM?
  • Why isn't my “Fizz Buzz” test in R working?
  • TFS - how do I sum child task hours to parent
  • Deploying a CodeRush plugin from the Community Site
  • How to pass solution folder as parameter in command line arguments (for debug)?
  • vectorized indexing/slicing in numpy/scipy?
  • Validate jQuery plugin, field not required
  • How can I tell a form not to dispose a particular control when it closes?
  • WPF version of .ScaleControl?
  • Servlet stops working on Tomcat server after some hits or time
  • ASP.NET MVC 2 Preview 2 - display directory list rather than home/index
  • Overlapping controls in Windows XP
  • IE7 and TinyMCE with Plone
  • Unity3D & Android: Difference between “UnityMain” and “main” threads?
  • Debugging ASP.NET on a built-in web server suddenly stops
  • Projection media query: browser support and workarounds?
  • swift auto completion not working in Xcode6-Beta
  • Get object from AWS S3 as a stream
  • Finding past revisions of files in StarTeam w/ .NET SDK / C#
  • Cross-Platform Protobuf Serialization
  • Does CUDA 5 support STL or THRUST inside the device code?
  • Validaiting emails with Net.Mail MailAddress
  • Hazelcast - OperationTimeoutException
  • Do I've to free mysql result after storing it?
  • Cannot Parse HTML Data Using Android / JSOUP
  • PHP: When would you need the self:: keyword?
  • C# - Getting references of reference
  • How to get Windows thread pool to call class member function?