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

How to write this linq query dynamically c#
Inconsistent behavior: The ntext and image data types cannot be used in WHERE, HAVING, GROUP BY, ON, or IN clauses
If statement code clarification
DataGridView Different cell formatting in same column possible?
Animate circle's fill using sine-wave
Overriding methods from Library Project
Reading CSV file in SSIS Script Component task
GroupBy equivalent for Windows Phone
How to save video into database using c#?
Sync Framework - file sync across domains
Selenium WebDriver C# and Omnibug
How to access a Dictionary values within a list by a class object?
System.Data.SqlClient.SqlException: Incorrect syntax near the keyword 'file'
SQL Server High Frequency Inserts
Register user only for my country
Best practice for caching values in large project

Categories

HOME
max
ssms
locale
formatting
query-string
overrides
ms-access-2010
uml-designer
gnuradio
zabbix
wavefront
minimax
guidewire
email-attachments
expo
google-closure-compiler
multibinding
game-engine
hapijs
descriptor
minimagick
combinations
fibonacci
ithit-webdav-server
chartist.js
aws-certificate-manager
knapsack-problem
jopendocument
federated
schedule
mplab
availability
openvms
powerbi-embedded
ragel
scip
framebuffer
julia-jump
ninja-forms
ssdp
definition
grails-2.0
ocpjp
jenkins-2
xmldom
ionic2-select
evaluation
jcifs
cc
totalview
jtextarea
codesniffer
timex
pyautogui
recursive-query
android-tabs
mongodb-3.3
virtualenvwrapper
git-cherry-pick
pyshark
ooad
jersey-test-framework
github-for-mac
magnet-uri
gitk
database-project
random-sample
musicbrainz
cakephp-2.6
data-representation
radgrid
service-broker
dockerpy
grunt-contrib-connect
go-gin
worklight-appcenter
ant-contrib
reactjs-native
windows-phone-store
myrrix
windows-scheduler
gwt2
gmail-imap
gssapi
m3u
github-archive
systrace
motorola-droid
utm
parallel-python
cinder
mediastreamsource
nstoolbar
database-agnostic
for-xml
software-update
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