Click here to Skip to main content
14,358,347 members

How to Release COM Interop Objects so the Called Program Can Exit

Rate this:
4.95 (10 votes)
Please Sign up or sign in to vote.
4.95 (10 votes)
13 Nov 2013CPOL
The COM Interop Runtime Callable Wrapper (RCW) is a "Black Box" that creates references to memory that need to be released before the called program will end.


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.


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

  1. Instantiate COM objects just before they are needed. 
  2. Use FinalReleaseComObject(obj) to release objects as soon as they are not needed. 
  3. Call FinalReleaseComObject on the most recently created objects first.
  4. 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

' Declare a COM object
Dim oApp As Excel.Application 
' Start Excel
oApp = DirectCast(CreateObject("Excel.Application"), Excel.Application)
oApp.Visible = True
' Open file
oApp.Workbooks.Open(strNewFilename, Excel.XlUpdateLinks.xlUpdateLinksNever)
' Run the CreateInventoryWorksheets Macro
' Macro is finished
' Close the file
' Tell Excel to quit
' This sample has only one COM object declared. 
' For programs with multiple COM objects declared, repeat the following two
' lines of code for each COM object in reverse order of creation.
oApp = Nothing ' Set each COM Object to Nothing
' After all of the COM objects have been released and set to Nothing, do the following:
GC.Collect() ' Start .NET CLR Garbage Collection
GC.WaitForPendingFinalizers() ' Wait for Garbage Collection to finish

C# Example

// Declare a COM object
Excel.Application oApp = default(Excel.Application);
// Start Excel
oApp = (Excel.Application)Interaction.CreateObject("Excel.Application");
oApp.Visible = true;
// Open file
oApp.Workbooks.Open(strNewFilename, Excel.XlUpdateLinks.xlUpdateLinksNever);
// Run the CreateInventoryWorksheets Macro
// Macro is finished
// Close the file
// Tell Excel to quit
// This sample has only one COM object declared. 
// For programs with multiple COM objects declared, repeat the following two
// lines of code for each COM object in reverse order of creation.
oApp = null; // Set each COM Object to null
// After all of the COM objects have been released and set to null, do the following:
GC.Collect(); // Start .NET CLR Garbage Collection
GC.WaitForPendingFinalizers(); // Wait for Garbage Collection to finish

Points of Interest

Below are links to several articles about COM Interop. Thanks to those authors for their fine articles.


  • 29 March 2013 - Initial version
  • 23 October 2013 - Minor edit
  • 12 November 2013 - Removed While Loop of ReleaseComObject and replaced with FinalReleaseComObject


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Mike Meinz
United States United States
I’m retired. When I started my career, programming projects consisted of plugging wires into plug boards to create punch card processing applications to be run on electrical accounting machine like the IBM 402, 407, 085, 088, 514, 519, etc. From there, I moved to writing SPS and Autocoder applications on an IBM 1401 with 4K of memory eventually upgraded to 16K of memory. After many years of migrating my skills to various languages on various hardware platforms, I became an Information Technology Director where I didn’t need to program anymore. So, starting in 1996, I volunteered my time with a local community cable television organization and built some applications to help them run their operations. Originally in Clipper Summer 1987 and later Clipper 5.2, I migrated and enhanced those applications to VB .NET 2003 in 2003. I retired from my full-time job in 2010. Since then, I have continued to support the local community cable tv organization's applications. In 2013, I migrated the VB .NET 2003 Solution to VB .NET 2012 so that it can run on 64-bit computers and interact with Microsoft Office 2010. The upgrade went smoothly. In mid 2013, I developed a VB .NET 2012 application for them to download election results data from the Secretary of State's web site, format the results and send them to a VizRT character generator for on-air display.

Comments and Discussions

PraiseSolved problem of COM server not being freed in a timely fashion Pin
Member 766294915-Sep-16 15:55
memberMember 766294915-Sep-16 15:55 
Suggestionsmall advise Pin
Dif-sam13-Nov-13 8:09
memberDif-sam13-Nov-13 8:09 
GeneralRe: small advice Pin
Mike Meinz13-Nov-13 8:51
memberMike Meinz13-Nov-13 8:51 
QuestionAt Last Pin
Michael_Davies11-Nov-13 12:07
memberMichael_Davies11-Nov-13 12:07 
QuestionQuery Pin
mukeshbraintech1-Aug-13 1:56
membermukeshbraintech1-Aug-13 1:56 
AnswerRe: Query Pin
Mike Meinz1-Aug-13 5:25
memberMike Meinz1-Aug-13 5:25 
GeneralMy vote of 5 Pin
_Vitor Garcia_8-Apr-13 6:43
member_Vitor Garcia_8-Apr-13 6:43 
Thank you
GeneralMy vote of 5 Pin
John Atten29-Mar-13 17:04
memberJohn Atten29-Mar-13 17:04 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Posted 29 Mar 2013


23 bookmarked