0

I have the following code, I wonder if I need to release ComObject manually or the garbage collector will release it for me?

wordApp = new Word.Application();
wordDoc = new Word.Document();
// some stuff

wordDoc.Close(null, null , null);
wordApp.Quit();
if(System.Runtime.InteropServices.Marshal.IsComObject(wordDoc))
    System.Runtime.InteropServices.Marshal.ReleaseComObject(wordDoc);
wordDoc = null;
if (System.Runtime.InteropServices.Marshal.IsComObject(wordApp))
    System.Runtime.InteropServices.Marshal.ReleaseComObject(wordApp);
wordApp = null;

Is the above code correct or shouldn't I use Marshal.IsComObject in the end?

Cindy Meister
  • 25,071
  • 21
  • 34
  • 43
Lion King
  • 495
  • 5
  • 14
  • Why do you have to call the same code twice? – FaizanHussainRabbani Feb 18 '18 at 16:54
  • 1
    You don't have to release it explicitly just setting it to null will GC it – sramalingam24 Feb 18 '18 at 16:58
  • 1
    There is no point in IsComObject(), you know it is one. ReleaseComObject() has little point as well, manual memory management rarely comes to a good end. But especially no point when closing the app, the CLR runs the finalizers before shutting down and that always takes care of any RCW. – Hans Passant Feb 18 '18 at 17:04
  • @HansPassant so remove the two if and just set them to null is sufficient in this case. – Lion King Feb 18 '18 at 17:08
  • Normally you shouldn't, but my experience with automating Office apps says that you would better. :-) – Nick Feb 18 '18 at 17:09
  • @Nick that's what I don't understand, from reading online some people suggest it is better to release it manually and some say it is better to leave it for the garbage collector. – Lion King Feb 18 '18 at 17:10
  • No, setting the variable to null is pointless as well. https://stackoverflow.com/a/17131389/17034 – Hans Passant Feb 18 '18 at 17:12
  • 1
    @LionKing, in case of Word / Office, I strongly recommend that you manually release them. If you leave it to the GC, it may not release the objects when you expect and that will lead to Word processes running in memory. That can cause also further trouble when you invoke Word next time. – Nick Feb 18 '18 at 17:13
  • @HansPassant I am getting different explanation from Nick (and some other articles I found online https://www.add-in-express.com/creating-addins-blog/2013/11/05/release-excel-com-objects/) – Lion King Feb 18 '18 at 17:33
  • 1
    This can be helpful https://stackoverflow.com/questions/47926633/release-com-when-developing-an-excel-addin/47927022#47927022. In short .. the recommendation from msdn documentation is not to do it explicitly in general, only if you really need to release the resources in specific order or timely manner. – vasil oreshenski Feb 18 '18 at 17:33
  • How do you think the answers will differ from your question from yesterday? – pinkfloydx33 Feb 18 '18 at 17:43
  • @pinkfloydx33 that was regarding the behavior of `List.Clear()` with `ComObject`, this is about if it is a good practice to release them manually or not. – Lion King Feb 18 '18 at 18:03
  • You should release them manually - *all* of them - in the reverse order they were instantiated. So `wordDoc` before `wordApp` for example. And it's also not a bad idea to invoke GC.Collect; GC.WaitForImpendingFinalizers; - twice in succession. (https://msdn.microsoft.com/en-us/library/office/aa679807(v=office.11).aspx) – Cindy Meister Mar 26 '18 at 19:12

0 Answers0