Is GC.SuppressFinalize guaranteed?

后端 未结 5 902
清酒与你
清酒与你 2020-12-31 23:10

My observation in practice has been that GC.SuppressFinalize does not always suppress the call to the finalizer. It could be that the finalizer gets called nontheless. I won

5条回答
  •  梦毁少年i
    2020-12-31 23:56

    One oddity you may be seeing is that the finalizer can still run even while an instance method is still running, so long as that instance method doesn't use any variables later on. So in your sample code, the Dispose method doesn't use any instance variables after the first line. The instance can then be finalized, even though Dispose is still running.

    If you insert a call to GC.KeepAlive(this) at the end of the Dispose method, you may find the problem goes away.

    Chris Brumme has a blog post about this, and I think there's another around somewhere...

提交回复
热议问题