c#


How to properly clean up interop objects in C#


This is a follow on question to
How to properly clean up excel interop objects in c#.
The gyst is that using a chaining calls together (eg. ExcelObject.Foo.Bar() ) within the Excel namespace prevents garbage collection for COM objects. Instead, one should explicitly create a reference to each COM object used, and explicitly release them using Marhsal.ReleaseComObject().
Is the behaviour of not releasing COM objects after a chained call specific to Excel COM objects only? Is it overkill to apply this kind of pattern whenever a COM object is in use?
It's definitely more important to handle releases properly when dealing with Office applications than many other COM libraries, for two reasons.
The Office apps run as out of process servers, not in-proc libraries. If you fail to clean up properly, you leave a process running.
Office apps (especially Excel IIRC) don't terminate properly even if you call Application.Quit, if there are outstanding references to its COM objects.
For regular, in-proc COM libraries, the consequences of failing to clean up properly are not so dramatic. When your process exits, all in-proc libraries goes away with it. And if you forget to call ReleaseComObject on an object when you no longer need it, it will still be taken care of eventually when the object gets finalized.
That said, this is no excuse to write sloppy code.
COM objects are essentially unmanaged code - and as soon as you start calling unmanaged code from a managed application, it becomes your responsibility to clean up after that unmanaged code.
In short, the pattern linked in the above post is necessary for all COM objects.

Related Links

The type or namespace name 'ThreadPoolTimer' could not be found
Join IQueryable with List on object from list
Exporting gridview with paging
Is it possible to pin a text on the top of the console c# [duplicate]
How to read simultaneously by several COM ports
How to delete rows with same value in datagridview
How to passing value to textbox in second form when object has been lunched
Make my datepicker required and remove it after selecting a date in c#
Microsoft Graph. DriveItem Id format?
DateTime parsing - unexpected result
Measuring mb sent per second with C# sockets? (Multithread)
C# inheritance multiple for different children
IList to JSON fails
Convert to Nested JSON C#
Sending List<Object> WCF POST
Export to excel is exporting check box to excel instead of string

Categories

HOME
pact
salesforce
packet
formatting
webvr
window.open
spotfire
data-modeling
azure-resource-manager
terminal
pdf-generation
markdown
genexus
vsixmanifest
oms
apache-httpclient-4.x
kairosdb
urllib2
email-attachments
jetbrains
accordion
webgl-earth
mattermost
concatenation
handle
functional-dependencies
datastax-enterprise
sasl
azure-table-storage
kundera
xlib
openoffice-impress
deferred
blockly
spring-oauth2
restler
wunderground
geocomplete
libconfig
mybatis-generator
jslider
ssdp
keychain
superclass
bootstrap-carousel
evaluation
spring-cloud-bus
lucee
fckeditor
grib
overhead
disassembly
iban
qt3d
timefield
ownership
turbolinks
gevent
hdr
class-design
geokit
magick.net
objectify
patching
diameter-protocol
abstract
wordpress-plugin
wiql
bootstrap-tabs
android-filterable
sparkcore
android-networking
log-analysis
jad
mathics
optimistic-locking
static-files
javadb
grunt-contrib-connect
apache-pivot
diffmerge
formbuilder
gnip
im4java
ccmenuitem
nspopupbutton
telerik-appbuilder
satisfiability
farpoint-spread
pidgin
sqldatasource
iwork
kuka-krl
flask-mongoengine
xcode6.1-gm-seed
google-mirror-api
google-voice
mandelbrot
symbian3
ftpwebrequest
reference-manual
phone-state-listener
certificate-revocation
cinder
rollover
xcdatamodel
chars
pitch-shifting
miniport
newspeak
dojox.gfx
onresize
j++
moores-law
ironpython-studio

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App