C# + COM Interop, deterministic release

ccominterop

COM objects usually have deterministic destruction: they are freed when the last reference is released.

How is this handled in C# – COM Interop? The classes don't implement IDisposable, so I see no way to trigger an explicit IUnknown::Release.

A casual test shows that unreferenced COM objects get collected lazily (i.e. the garbage collector is triggering the release). What should I do for OCM objects that need to be released aggresively? (e.g. holding large or shared critical ressources)?

Original problem: We have a C# application heavily using a COM library, and it is leaking like mad. It seems that the problems is "between" the C++ and the C# code (we have access to both), but we can't nail it down.

Best Answer

You can manipulate COM interop references using the System.Runtime.InteropServices.Marshal class. Specifically you may want to have a look at Marshal.ReleaseComObject.

Related Topic