Introduction
If COM Interop objects are not properly released in your .NET code, the program that your .NET program is communicating with via COM Interop will remain in the Windows operating system task list without a visible window. You can see it in the Details tab of Task Manager.
Background
The common language runtime exposes COM objects through a proxy called the Runtime Callable Wrapper (RCW). To a .NET application, a RCW looks like an ordinary .NET object. It handles calls between a .NET client and a COM object (e.g. The EXCEL.EXE application and objects within EXCEL.EXE) marshaling arguments and return values between the .NET client and the COM object; converting them when necessary.
For each COM object, the .NET runtime creates a single RCW regardless of the number of references to that object. A reference count is incremented each time a new reference is made to the RCW of an instantiated COM object. Calling a "Quit
" or "Exit
" method followed by a "Dispose
" method on a COM object will not free up all of the references. The example code in this tip will ensure that all references are released so that the called COM program will end properly.
Coding Model for COM Interop
- Instantiate COM objects just before they are needed.
- Use
FinalReleaseComObject(obj)
to release objects as soon as they are not needed.
- Call
FinalReleaseComObject
on the most recently created objects first.
- Use the .NET Garbage Collection methods to force garbage collection.
COM Interop Example
My original example is VB .NET code. I used the developerFusion Convert VB .NET to C# tool to convert my VB .NET code to the C# code included in this tip.
VB Example
Dim oApp As Excel.Application
oApp = DirectCast(CreateObject("Excel.Application"), Excel.Application)
oApp.Visible = True
Application.DoEvents()
oApp.Workbooks.Open(strNewFilename, Excel.XlUpdateLinks.xlUpdateLinksNever)
Application.DoEvents()
oApp.Run("CreateInventoryWorksheets")
Application.DoEvents()
oApp.ActiveWorkbook.Close(True)
Application.DoEvents()
oApp.Quit()
Application.DoEvents()
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(oApp)
oApp = Nothing GC.Collect() GC.WaitForPendingFinalizers()
C# Example
Excel.Application oApp = default(Excel.Application);
oApp = (Excel.Application)Interaction.CreateObject("Excel.Application");
oApp.Visible = true;
Application.DoEvents();
oApp.Workbooks.Open(strNewFilename, Excel.XlUpdateLinks.xlUpdateLinksNever);
Application.DoEvents();
oApp.Run("CreateInventoryWorksheets");
Application.DoEvents();
oApp.ActiveWorkbook.Close(true);
Application.DoEvents();
oApp.Quit();
Application.DoEvents();
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(oApp);
oApp = null; GC.Collect(); GC.WaitForPendingFinalizers();
Points of Interest
Below are links to several articles about COM Interop. Thanks to those authors for their fine articles.
History
- 29 March 2013 - Initial version
- 23 October 2013 - Minor edit
- 12 November 2013 - Removed While Loop of ReleaseComObject and replaced with FinalReleaseComObject