Do I need to call Marshal.ReleaseComObject in C # 4 when running COM?

I have VS2010 and added a link to the COM library for my project, and VS built in the primary interface inside the project.

If I refer to objects from the COM library and I want to get rid of them quickly without waiting for the GC, do I need to call ReleaseComObject?

+3
source share
2 answers

Marshal.ReleaseComObject provides a way to immediately delete references to this COM object because it is consumed inside managed code (since it frees the underlying IUnknown from the RCW that holds it).

As Hans notes, the overall right course is to simply destroy your object so that the CLR and GC can destroy COM objects at the right time.

However, in situations where you need an immediate action (the COM object contains expensive / limited resources, or perhaps during shutdown, where the sequence is very complex), calling ReleaseComObject may be correct.

Martyn

+3
source

There are two types of approaches to clearing memory.

  • Link Counting Algorithms
  • Garbage collection

COM components use a reference counting algorithm to recover memory. Whenever you do not need a com component link, you can call it. But usually, my approach creates a virtual stack and removes links like the source code below in C # .. NET ensures that RCW is alive, COM components come to life. When RCW is garbage collection, the release method is called. There is no need to invoke the release in your code. But this does not affect gc loops.

void DoSth { { RunTimeCallableWrapper area for the code ReleaseComObject } } 
0
source

Source: https://habr.com/ru/post/1489677/


All Articles