views:

51

answers:

2

I have a c# .NET assembly that gets called from a C++ COM client. When the client process explicitely release the COM object, the CCW release it'sinternal reference, thus making the .NET object eligible for garbage collection.

What if the .NET object has a static constructor that open a log file ? Is there an elegant solution to close the log file without an explicit request from the client ?

TIA.

A: 

If you don't want to modify the COM client, then you're out of luck for any determinate close magic. Ultimately the file handle will be closed at some time in the future (assuming it's a FileStream) when the handle's Finalizer is called by the GC after your CCW loses its GC root(s.)

-Oisin

x0n
Would the Finalizer be called when the client process ends ?
omatrot
If this is an in-process COM server: yes.
Hans Passant
After some testing using AppDomain.ProcessExit EventHandler seems to be a good choice to close the log file.
omatrot
+1  A: 

After some testing using AppDomain.ProcessExit EventHandler seems to be a good choice to close the log file.

omatrot