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

receiving string from c# to Java mistakes
No endpoint listening wcf/rest
How to add NT AUTHORITY\IUSR to Administrators group
Combobox in Syncfusion XlsIO
Creating a Strong Name dynamically, without Sn.exe
Why is my locally declared variable not being recognized in the finally block?
Comparing two different types with Entity Framework
loginview shows “welcome system-name/administrator” and logout for the first time page load
How to abort another thread in .NET, when said thread is executing Console.ReadLine?
OrderBy / ThenBy looping - nested Lists in C#
How to login external web site from asp.net web page? [closed]
ListPicker for WPF?
How to notify user of new message?
WPF Treeview Bindings
How do you iterate through JSON object in an ASP.NET MVC controller?
Explicitly specifying the TaskScheduler for an implicitly scheduled method

Categories

HOME
airflow
logging
indexing
system-calls
network-programming
cors
phantomjs
singleton
snmp
sdk
msbi
confluence-rest-api
liquidsoap
markdown
scapy
rethinkdb
ldap-query
jetbrains
comma
openstack-horizon
infopath
md2
conditional-formatting
hl7-fhir
single-page-application
computation-theory
android-wifi
fibonacci
gnu-screen
poisson
android-cursorloader
ios9
ios-autolayout
flickr
mootools
gitosis
dom4j
flex3
perf
queryover
linkurious
server-side-swift
ui-grid
mixed-models
php-mongodb
logan-square
spring-ide
anonymous-function
nao-robot
runtime-permissions
fluent-nhibernate-mapping
fckeditor
boost-regex
opticalflow
sharing
gevent
jigsaw
particle-system
cherry-pick
nimbus
abstract
std
maven-antrun-plugin
biginsights
web-standards
mergecursor
jad
cl.exe
attiny
teamstudio-unplugged
optimistic-locking
pysvn
worklight-appcenter
slimscroll
data-layers
mysql-error-1146
lastinsertid
oc4j
xrandr
source-depot
backbone-views
pudb
gwt2
motordriver
erlide
httppostedfilebase
parallel-python
windows-xp-embedded
youtube.net-api
functional-specifications
hwnd
hs-err
noise-reduction
rollover
table-valued-parameters
pixel-bender
user-preferences

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