COM object survives when it shouldn't - why? RRS feed

  • Question

  • Hi,

    I have searched the forums, posted queries in other areas, all to no avail.  I call System.Runtime.InteropServices.Marshal.ReleaseComObject for every variable that was used to reference the COM object, then set them to null.  I then call GC.Collect() and then GC.WaitForPendingFinalizers(). 

    No mater how I monitor it, everything seems to work fine.  However, when this is complete (and even after the application is exited) the COM instance is still open in the Windows Task Manager.

    The only solution I have that works is to programmatically check the processes and kill the COM instance that I created.  I don't like this approach as the COM object should go away properly.

    I have posted the code and it has been picked over by a bunch of people in other forums, so I am fairly confident I have not left object references unresolved.

    Ok, that said, this is while trying to use Excel 2007.  The code was working just fine up until some period in the not to distant past (it took awhile for me to figure out this was happening.) 

    My guess is that it is either associated with Excel 2007 (which was just recently pushed out to the computers) or due to some policy or add-in that has been pushed out (I work at a very security conscious defense contractor).  Given the amount of things I have tried, I am leaning towards the latter, but I don't know how to investigate this and would appreciate any ideas.

    If I programmatically open excel and close it, it works fine.  If I open and create a document, then tell it to close that item without saving, excel won't go away.  Yes, I make sure to save the object reference created when I do so and release it per above. 

    I even have a loop that tries to go through the main Excel instance to re-obtain object handles to any possible open worksheet/workbook and specifically ReleaseComObject those.

    But Excel just won't go away.

    I would be happy to post the code (I now have a bunch of different versions, based on various approaches I have tried.)  Most examples are very simple. Instantiate Excel, add a workbook, close the workbook without saving, then a bunch of code trying to make it go away.

    Any ideas/help would be appreciated.


    Thursday, March 25, 2010 5:07 PM


All replies

  • Random thoughts: You could try Marshal.FinalReleaseComObject on the objects.  If you have any call-back events, they sometimes cause objects to stay around. 
    Phil Wilson
    Thursday, March 25, 2010 10:20 PM
  • Phil,

    Thanks.  I create an instance of Excel, tell it to create a new workbook (assinging the return value to a variable), then tell it to close the workbook without saving.  I Marshal.FinalReleaseComObject the workbook variable and set it to nothing.  Then I tell Excel to quit and do the same with it.  I have even tried adding calls to the GC to force a cleanup.  But none of these have worked.  Excel is still happily running in the task manager.  And none of these actions should initiatiate a call back (that I know about at least.)

    Any other ideas?



    Tuesday, March 30, 2010 4:30 PM
  • Hi all,

    If you want the answer, see this thread: and find the reply (by me) marked as answer.



    • Marked as answer by FletcherJ Tuesday, June 1, 2010 5:20 PM
    Tuesday, June 1, 2010 5:19 PM