I long time ago, I asked about [WayBack] Why doesn’t WINWORD.EXE quit after Closing the document from Delphi?.
It turns out that question is a lot harder in .NET than it is in Delphi. I already had a gut feeling of this when at clients I saw many more .NET applications leaking WINWORD.EXE stray processes than Delphi applications, even though both kinds were calling Quit on the Word application object.
Delphi has a deterministic way of coping with interfaces (hence you can do a One-liner RAII in Delphi, or make a memento): Interface references are released at the end of their scope.
.NET has non-deterministic finalization of the Common Language Runtime (CLR) and has Runtime Callable Wrappers (RCWs) around your COM references which are sometimes created “on the fly”.
The combination of non-deterministic finalization and RCWs can be very confusing, so lets start with the parts first. Read the rest of this entry »