Release COM objects immediately

Release COM objects immediately

When you use a COM or COM+ from a .NET application and then you set the object reference to Nothing (or let it go out of scope), the object isn’t released immediately. The reason: your program communicates with the COM object through a Runtime Callable Wrapper (RCW) object, which is responsible for the real object’s lifetime, thus the object will be actually released only when the RCW is garbage collection, which might happen several minutes later or even only when the entire application is shut down.

In most cases this behavior is acceptable, but at times it may be essential that the COM object is explicitly released when the .NET application doesn’t need it any longer. You can do this by calling the ReleaseComObject static method of the Marshal class (in the System.Runtime.InteropServices namespace):

' Create the objectDim obj As New MyComObject()' Use it' ....' Explicitly release itSystem.Runtime.InteropServices.Marshal.ReleaseComObject(obj)obj = Nothing

See also  How to Block Websites During Certain Hours on a Mac Computer

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist