Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Language: VB7
Expertise: Beginner
Jan 1, 2003



Building the Right Environment to Support AI, Machine Learning and Deep Learning

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 object
Dim obj As New MyComObject()
' Use it
' ....
' Explicitly release it
obj = Nothing
Francesco Balena
Thanks for your registration, follow us on our social networks to keep up-to-date