COM Callable Wrapper and .NET object lifetime
I have a c# .NET assembly that gets called from a C++ COM client. When the client process explicitely release the CO开发者_开发知识库M 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.
After some testing using AppDomain.ProcessExit EventHandler seems to be a good choice to close the log file.
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
精彩评论