Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Proper disposal of COM interop objects in C# particularly MS Office applications

I am developing an application that relies heavily on multiple Microsoft Office products including Access, Excel, Word, PowerPoint and Outlook among others. While doing research on interop I found out that starting with VS2010 and .NET 4, we thankfully no longer have to go through the nightmares of PIAs.

Furthermore, I have been reading a lot of articles on proper disposal of objects, the most sensible one seemed to be this one.

However, the article is 5 years old and there are not many authoritative publications on the subject AFAIK. Here is a sample of code from the above link:

' Cleanup:
GC.Collect()
GC.WaitForPendingFinalizers()
GC.Collect()
GC.WaitForPendingFinalizers()

Marshal.FinalReleaseComObject(worksheet)

oWB.Close(SaveChanges:=False)
Marshal.FinalReleaseComObject(workbook)

oApp.Quit()
Marshal.FinalReleaseComObject(application)

What I want to know is by today's standards, how accurate is this and what should I look out for if I expect to support my application for the coming few years?

UPDATE: A link to some credible articles would be highly appreciated. By the way, this is not a server side application. This will be running in computer labs where we have users interact with office products that we instantiate for them.

FOUND IT: This three-part article is probably the closest to an authoritative account I would expect to find.

like image 713
Raheel Khan Avatar asked May 10 '12 15:05

Raheel Khan


People also ask

How do you release COM objects?

The ReleaseComObject method decrements the reference count of an RCW. When the reference count reaches zero, the runtime releases all its references on the unmanaged COM object, and throws a System. NullReferenceException if you attempt to use the object further.

How do you Dispose an object in C++?

When delete is used to deallocate memory for a C++ class object, the object's destructor is called before the object's memory is deallocated (if the object has a destructor). If the operand to the delete operator is a modifiable l-value, its value is undefined after the object is deleted.

What does Dispose () do in C#?

In the context of C#, dispose is an object method invoked to execute code required for memory cleanup and release and reset unmanaged resources, such as file handles and database connections.

How do you use the Dispose method?

The Dispose method performs all object cleanup, so the garbage collector no longer needs to call the objects' Object. Finalize override. Therefore, the call to the SuppressFinalize method prevents the garbage collector from running the finalizer. If the type has no finalizer, the call to GC.


2 Answers

Objects should be disposed automatically by the GC after the object goes out of scope. If you need to release them sooner, you can use Marshal.ReleaseComObject http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.marshal.releasecomobject.aspx

like image 122
Peter Ritchie Avatar answered Sep 22 '22 16:09

Peter Ritchie


Depending on the version of Office you are controlling through interop, and the circumstances that occur on the server, yes, you may need to do some serious convolutions to get rid of a running instance of one of the Office applications. There are unpredictable (and unnecessary) dialogs which, for example, Word will open when it is not interactive (None of the Office applications are particularly well designed for unattended execution, thus the recommendation that even with the latest versions that they not be installed for server-side use).

For server cases, consider using Aspose's stack rather than Office, or another of the alternatives. The interactive, local use cases, you may need that "extra vigorous killing" on occasion since the Office automation servers are particularly badly behaved unmanaged objects.

like image 45
Tetsujin no Oni Avatar answered Sep 22 '22 16:09

Tetsujin no Oni