COM Callable Wrapper 和 .NET 对象生命周期
我有从 C++ COM 客户端调用的 c# .NET 程序集。当客户端进程显式释放 COM 对象时,CCW 会释放它的内部引用,从而使 .NET 对象符合垃圾回收的条件。
如果 .NET 对象具有打开日志文件的静态构造函数怎么办?是否有一个优雅的解决方案可以在没有客户端明确请求的情况下关闭日志文件?
TIA。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
经过一些测试后,使用 AppDomain.ProcessExit EventHandler 似乎是关闭日志文件的不错选择。
After some testing using AppDomain.ProcessExit EventHandler seems to be a good choice to close the log file.
如果您不想修改 COM 客户端,那么您就无法使用任何确定的关闭魔法。最终,当 CCW 丢失 GC 根后 GC 调用句柄的 Finalizer 时,文件句柄将在将来的某个时间关闭(假设它是 FileStream)。
-Oisin
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