O'Reilly logo

C# Cookbook by Jay Hilyard, Stephen Teilhet

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

3.30. Releasing a COM Object ThroughManaged Code

Problem

You need to release a COM object from managed code without forcing a garbage collection to occur.

Solution

Use the static ReleaseComObject method of the Marshal class:

int newRefCount = System.Runtime.InteropServices.Marshal.ReleaseComObject(COMObj);

where COMObj is a reference to the runtime callable wrapper (RCW) of a COM object.

Discussion

If the COM object is holding on to resources that need to be released in a timely manner, we will want to decrement the reference count on the COM object as quickly as possible, once we’ve finished using the COM object and have set it to null. The GC needs to run in order to collect the unreferenced RCW around our COM object, thereby decrementing the reference count on the COM object. Unfortunately, there is no guarantee that the GC will run in order to collect the RCW anytime in the near future.

To solve this problem, we could call GC.Collect ourselves to try to free the RCW, but this might be overkill. Instead, use the ReleaseComObject method to manually force the RCW to decrement its reference count on the COM object without having to force a collection to occur.

The static ReleaseComObject method returns an int indicating the current reference count contained in the RCW object after this method has finished decrementing its reference count. Remember that this method decrements the reference count contained in the RCW, not the COM object’s reference count. When the RCW reference count goes ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required