1

I have a list containing different ComObjects. If I do myList.Clear() would it be enough or should I loop through it and does this.

for (int i = 0; i < myList.Count; ++i)
     if (System.Runtime.InteropServices.Marshal.IsComObject(myList[i]))
         System.Runtime.InteropServices.Marshal.ReleaseComObject(myList[i]);

     myList.Clear();
Lion King
  • 495
  • 5
  • 14
  • Why do you feel it is necessary to manipulate the reference count of the callable wrapper? Or, better question: **what do you believe that ReleaseComObject does?** Odds are pretty good that you have false beliefs about it. – Eric Lippert Feb 16 '18 at 23:40
  • @EricLippert so in this case just calling clear would release them? – Lion King Feb 16 '18 at 23:46
  • @LionKing: Why do you care? Let the garbage collector do its work. Is there some reason why you feel like you ought to be doing the GC's job for it? Why are you even clearing the list? And you still haven't said what you believe that RCO() does; can you explain what you think it does? If you don't understand what RCO does then you should stop writing COM interop code until you do understand it. You can really, really screw things up if you use it wrong. – Eric Lippert Feb 16 '18 at 23:48
  • @EricLippert This is what I do currently. I am adding `Microsoft.Office.Interop.Outlook.Folder` to `myList`. Then I perform some logic on them and at the end I am not sure what to do with them (this is where my question comes). Should I clear them for the next time or loop through and release them (I fill the list later on multiple times). – Lion King Feb 16 '18 at 23:56
  • @EricLippert to wrap it up, I shouldn't do anything to it and let the garbage collector do its thing. Can you put your comment as an answer so I can accept it? – Lion King Feb 17 '18 at 00:01

1 Answers1

4

It's not broken, so don't fix it. If you are not having a problem with the collection semantics of a COM object, let the garbage collector do its work. If you are having a problem, then carefully describe the problem and what you understand about COM interop in C#.

In particular, calling ReleaseComObject to manage the RCW or worse, Release on the actual pUnknown is a for interop experts only sharp tool. Incorrect use of these tools can get you into any number of horrible situations full of crashes and memory corruptions. (If you ever see a "COM object is detached from its RCW" exception, you know someone who thought they knew what they were doing called ReleaseComObject wrong.)

If you don't understand what an RCW is or what it is for or how it differs from the underlying COM object, stop writing interop code until you do understand it. The memory safety system is designed for your safety and convenience; don't mess with it unless you can describe all of the consequences of doing so.

I note that your actual question was never answered:

Does clearing a List containing ComObject, release all of them in c#?

Of course not. Why should it? It clears the list. Clear the list when your program logic requires the list to be cleared. Don't clear a list because you believe that doing so has some knock-on effect on other code!

Eric Lippert
  • 647,829
  • 179
  • 1,238
  • 2,067