问题标签 [finalizer]
For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.
c# - 未调用终结器
我在 C# 中有一个类,我想在处理我的类时正确关闭一些通信端口。但是,当我退出程序时,永远不会调用终结器。这是为什么?难道我做错了什么?
我正在手动调用 dispose,它会通过并关闭所有通信。这也没有被解雇。
这是我正在使用的终结器:
c# - 垃圾收集和终结器:更精细的点
在回答关于 SO 的另一个问题* 以及随后的评论讨论时,我遇到了一个我不清楚的问题。
在我误入歧途的任何地方纠正我......
当垃圾收集器收集一个对象时,它会在单独的线程上调用该对象的终结器(除非终结器已被抑制,例如通过Dispose()
方法)。收集时,GC 会挂起除触发收集的线程之外的所有线程(后台收集除外)。
什么不清楚:
- 垃圾收集器在收集之前是否等待终结器在该对象上执行?
- 如果不是,它是否在终结器仍在执行时取消挂起线程?
- 如果它确实等待,如果终结器遇到被挂起线程之一持有的锁会发生什么?终结器线程是否死锁?(在我的回答中,我认为这是一个糟糕的设计,但我可能会看到可能发生这种情况的情况)
* 链接到原始问题:
.NET GC Accessing a synchronized object from a finalizer
winforms - What causes the InvalidComObjectException: "COM object that has been separated from its underlying RCW cannot be used."?
I've looked at the various questions that mention this specific exception (this question lists many of them, which I've visited). Also, I have the same general question as this post, but in a different context, so the answer is not helpful to me.
The context
I have a class derived from AxWindowsMediaPlayer
that is owned by a class called View
, which is within a Panel
, within a Workspace
. I recently asked a question around this situation, but that question was directed towards whether my workaround for this problem is okay. The background from that question is relevant here:
When a View
gets disposed, a method called Synchronize()
will get called on all of the remaining View
objects. For the View
that contains the AxWindowsMediaPlayer
, it calls videoPlayer.Error.clearErrorQueue()
.
The problem
When I call Dispose()
at the top level (Workspace.Dispose()
), if another View
gets disposed and then causes Synchronize()
to be called on the remaining View
objects, the View
containing the AxWindowsMediaPlayer
class throws an exception on the videoPlayer.Error.clearErrorQueue()
line, stating:
InvalidComObjectException: COM object that has been separated from its underlying RCW cannot be used.
I'm puzzled by how the AxWindowsMediaPlayer
is getting separated from its underlying RCW (Runtime Callable Wrapper). I've read this article that talks about this exception and the dangers of calling Marshal.ReleaseComObject()
. I am not calling this method explicitly. I have put breakpoints at the Dispose
methods of Panel
and View
and VideoPlayerControl
(derives from AxWindowsMediaPlayer
) classes, but none of those get hit before the exception happens.
My workaround is to make sure that the View
with the media player always gets disposed first. This was the motivation behind my previous question. But I'd like to understand how this is happening so I can see whether this is something I need to fix. Who is causing the AxWindowsMediaPlayer
to be separated from its RCW before Dispose
is getting called on the parent class?
My guess is that the AxWindowsMediaPlayer
finalizer is getting called by the GC, but I don't understand what is triggering it. For some reason, calling Dispose
at the higher level is causing Marshal.ReleaseComObject
to get called under the floor. Can someone enlighten me?
c# - 为什么 C# 析构函数需要有 XML 文档?
我喜欢尽量减少我的 C# 代码的注释和 Xml 文档。宁愿在可能的情况下使代码自记录。
但是,如果我不对公共类的析构函数添加 Xml 注释,C# 编译器会发出警告。为什么是这样?
是否有一些有用的信息我应该在这里放入 Xml 注释中,而我一直没有这样做。我从来没有发现自己需要阅读关于析构函数的评论。编译器只是过于热心了吗?
c# - 如何在 C# 中调用自定义跟踪侦听器上的 dispose
我编写了一个扩展 TextWriterTraceListener 的自定义跟踪侦听器。
现在,如何在侦听器上调用 Dispose()?我通过我的项目的 app.config 添加它。
尝试在终结器中添加调用 Dispose(false),但它没有被调用。奇怪的是,它在 VS 2010 中被调用,但在我运行应用程序时没有被调用,但我知道不能保证 GC 收集。(基本上这在 nunit 测试中用于记录来自测试的 System.Net.Socket 调用,我需要在所有测试运行后进行一些后期处理并将其写入日志。我在 Dispose 中添加了这部分( ) )
App.config 片段:
一些代码片段:
c# - 使用解构函数错了吗?
我有一些文件写入 c# 中的临时位置。然后将它们呈现为 pdf 缩略图。当对象被销毁时,我希望清理该位置,那么在这种情况下可以使用解构函数吗?
f# - F# 等效的析构函数
我正在将一个将非托管库包装到 F# 的 C# 类翻译。我遇到了重写以下析构函数的看似简单的问题。
我此时的简化 F# 代码如下:
如何修改此 F# 代码以具有相同的行为?我对 F# 析构函数的搜索在我拥有的书籍或网络上都没有结果。
谢谢你。
java - finalize 方法如何与 java 中的幻像引用一起工作
嗨,我对幻影参考有一个疑问。我所理解的 finalize 方法是在对象进行垃圾收集之前调用的。但是有时如果对象不符合垃圾回收条件,则 finalize 方法将不会执行。
现在谈论这个 finalize 方法将被调用时的幻像引用。
总是在幻像引用中调用 finalize。
我对此很困惑。请帮我。
c# - C#:在终结器中,如何确定应用程序是否正在关闭?
我有一个在应用程序关闭期间似乎总是失败的终结器。我认为这是因为它持有一些在那时不再有效的原生资源。有没有办法在析构函数/终结器中判断它是否由于应用程序关闭而被调用?
谢谢!