COM Interoperability Life time Management

A

Ashish Sheth

Hi all gurus,
How .NET manages the life time of a COM object that is called from a
..NET assembly using COM interoperability? My doubt is, COM uses
reference counting for life time management while .NET objects are
garbage collected. so when a COM object will not have any references to
it in .NET assembly it should be released from the memory but the actual
..NET proxy may not be released untill the garbage collector runs. so
when actually the COM object will be released from the memory?

thanks and regards,
Ashish
 
V

Vadim Melnik

Hi,
How .NET manages the life time of a COM object that is called from a
NET assembly using COM interoperability? My doubt is, COM uses
reference counting for life time management while .NET objects are
garbage collected. so when a COM object will not have any references to
it in .NET assembly it should be released from the memory but the actual
NET proxy may not be released untill the garbage collector runs. so
when actually the COM object will be released from the memory?

..NET creates proxy (RCW - runtime callable wrapper) for COM object, and
increments reference count for underlying object. By default COM object will
be released when RCW object will be garbage collected. You can force garbage
collector by System.GC.Collect method call. Sometimes it's necessary release
COM object right now - Marshal.ReleaseComObject from
System.Runtime.InteropServices namespace will help. It should be called in
loop, till returned value reaches zero. By then RCW releases COM object, and
probably future usage of RCW will throw exception.

...
Regards,
Vadim.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top