在 this question @Jon skeet 引用了这个 old blog post由权威克里斯布鲁姆。
我想知道,我是否需要关注所有对 GC.SuppressFinalize(this)
的调用?与 GC.KeepAlive(this)
为了避免在处理程序在大量多线程应用程序中运行期间可以调用终结器的奇怪竞争条件?
如果是这样,你能想出一个暴露这个错误的示例程序吗?
最佳答案
号码GC.KeepAlive
实际上什么也没做;其目的是“欺骗”运行时以防止在方法开始和调用 GC.KeepAlive
之间对特定对象进行垃圾收集。 .
任何方法调用都会以这种方式使对象保持事件状态:您可以将其传递给 Console.WriteLine
, 调用ToString
, 甚至... GC.SuppressFinalize
.
(或者正如 MSDN 所说:
The KeepAlive method performs no operation and produces no side effects other than extending the lifetime of the object passed in as a parameter.)
关于.net - 我需要在我的 Dispose 方法中调用 GC.KeepAlive(this) 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/816818/