John,
You don't have to manually collect it, but you should release the object
as soon as possible. There are two things that are going on here that you
have to be aware of.
First, there is the actual COM object in memory. There is also the
Runtime Callable Wrapper (RCW) which is the managed representation of the
COM object.
When you pass the RCW (what you use in your .NET code) to the static
ReleaseComObject method on the Marshal class, the runtime callable wrapper
will decrement the reference count on the COM object, and remove the
association between the COM object and the wrapper. It is very possible
that this object will still survive if the reference count is higher than
one. However, this isn't really your concern, just like it isn't when
programming in the unmanaged realm. In the unmanaged realm, you are
responsible for releasing the references that you are responsible for. So,
in the same sense, call ReleaseComObject for the references you know you are
responsible for.
Then that leaves the RCW. However, once the reference to the COM object
is released, this object is like any other, and has no unmanaged resource
associated with it. Assuming no other .NET object has a reference to it, it
will be garbage collected.
Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)
John Sun said:
Dear Group Gurus,
If I use a COM class in my C# code, will the memory used by COM object be
garbage collected, or do I have to manually collect it.
Thanks,
John