Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does *every* Excel interop object need to be released using Marshal.ReleaseComObject?

Edit

Please see also How do I properly clean up Excel interop objects?. I recently came across this question, and it provided a lot of insight into the problem of how to properly dispose of COM objects. Definitely check beyond the first (marked) answer, because the other answers go beyond the simple "don't use two dots" and "use ReleaseComObject for every com object" advice.

I revisited this question in the first place because I realized that, despite being very thorough about registering and disposing all my COM objects, my Excel instances still weren't being properly disposed. It turns out, there are ways COM objects can be created that are completely non-obvious (i.e., you can miss COM objects even if you never use two dots). In addition, even if you are thorough, if your project grows beyond a certain size, the chance of missing a COM object approaches 100%. And it can be very hard to find the one you missed when that happens. The answers to the question linked above provide some other techniques for making sure the Excel instance definitely gets closed. Meanwhile, I've made a small (but significant) update to my ComObjectManager (below) to reflect what I learned from the question linked above.

Original Question

I've seen several examples where Marshal.ReleaseComObject() is used with Excel Interop objects (i.e., objects from namespace Microsoft.Office.Interop.Excel), but I've seen it used to various degrees.

I'm wondering if I can get away with something like this:

var application = new ApplicationClass(); try {     // do work with application, workbooks, worksheets, cells, etc. } finally {     Marashal.ReleaseComObject(application) } 

Or if I need to release every single object created, as in this method:

public void CreateExcelWorkbookWithSingleSheet() {     var application = new ApplicationClass();     var workbook = application.Workbooks.Add(_missing);     var worksheets = workbook.Worksheets;     for (var worksheetIndex = 1; worksheetIndex < worksheets.Count; worksheetIndex++)     {         var worksheet = (WorksheetClass)worksheets[worksheetIndex];         worksheet.Delete();         Marshal.ReleaseComObject(worksheet);     }     workbook.SaveAs(         WorkbookPath, _missing, _missing, _missing, _missing, _missing,         XlSaveAsAccessMode.xlExclusive, _missing, _missing, _missing, _missing, _missing);     workbook.Close(true, _missing, _missing);     application.Quit();     Marshal.ReleaseComObject(worksheets);     Marshal.ReleaseComObject(workbook);     Marshal.ReleaseComObject(application); } 

What prompted me to ask this question is that, being the LINQ devotee I am, I really want to do something like this:

var worksheetNames = worksheets.Cast<Worksheet>().Select(ws => ws.Name); 

...but I'm concerned I'll end up with memory leaks or ghost processes if I don't release each worksheet (ws) object.

Any insight on this would be appreciated.

Update

Based on the answers so far, it sounds like I really do need to release every single com object I create. I took the opportunity to build a ComObjectManager class to make it a little easier to deal with this headache. You have to remember to use the Get() method each time you instantiate a new com object, but if you do, it will take care of everything else for you. Please let me know if you see any problems with it (or edit and leave a comment if you are able). Here's the code:

public class ComObjectManager : IDisposable {     private Stack<object> _comObjects = new Stack<object>();      public TComObject Get<TComObject>(Func<TComObject> getter)     {         var comObject = getter();         _comObjects.Push(comObject);         return comObject;     }      public void Dispose()     {         // these two lines of code will dispose of any unreferenced COM objects         GC.Collect();         GC.WaitForPendingFinalizers();          while (_comObjects.Count > 0)             Marshal.ReleaseComObject(_comObjects.Pop());     } } 

Here's a usage example:

public void CreateExcelWorkbookWithSingleSheet() {     using (var com = new ComObjectManager())     {         var application = com.Get<ApplicationClass>(() => new ApplicationClass());         var workbook = com.Get<Workbook>(() => application.Workbooks.Add(_missing));         var worksheets = com.Get<Sheets>(() => workbook.Worksheets);         for (var worksheetIndex = 1; worksheetIndex < worksheets.Count; worksheetIndex++)         {             var worksheet = com.Get<WorksheetClass>(() => (WorksheetClass)worksheets[worksheetIndex]);             worksheet.Delete();         }         workbook.SaveAs(             WorkbookPath, _missing, _missing, _missing, _missing, _missing,             XlSaveAsAccessMode.xlExclusive, _missing, _missing, _missing, _missing, _missing);         workbook.Close(true, _missing, _missing);         application.Quit();     } } 
like image 651
devuxer Avatar asked May 28 '10 01:05

devuxer


People also ask

When to use Marshal ReleaseComObject?

You should use this method to free the underlying COM object that holds references to resources in a timely manner or when objects must be freed in a specific order. Every time a COM interface pointer enters the common language runtime (CLR), it is wrapped in an RCW.


1 Answers

I believe you would have to call ReleaseComObject on each COM object. Since they're not garbage-collected, the parent-child hierarchy doesn't really come into the equation: even if you release the parent object it does not decrement the reference count on any child objects.

like image 190
EMP Avatar answered Sep 24 '22 07:09

EMP