为什么"最后确定方法不应该引用任何其他对象"? [英] Why "Finalize method should not reference any other objects"?

查看:129
本文介绍了为什么"最后确定方法不应该引用任何其他对象"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在思考为什么我们建议我们应该的的版本管理内部敲定的资源。
如果你看到在 http://msdn.microsoft.com代码示例/en-us/library/system.gc.suppressfinalize.aspx ,并搜索字符串的Dispose(BOOL处置)执行两个不同的场景和阅读评论,你会明白我的意思。

I have been pondering why it is recommended that we should not release managed resources inside finalize. If you see the code example at http://msdn.microsoft.com/en-us/library/system.gc.suppressfinalize.aspx , and search for string "Dispose(bool disposing) executes in two distinct scenarios" and read that comment, you will understand what I mean.

我唯一能想到的可能是,它可能有一些做的事实,这是无法预测何时终结将调用。
有谁知道正确的答案?

Only possibility I can think of is that it probably has something to do with the fact that it is not possible to predict when finalizer will get called. Does anyone know the right answer ?

感谢,
米沙勒

thanks, mishal

推荐答案

如果你引用从另一个终结的对象,你不知道的的对象已经敲定 - 这将如何表现,如果它的的定稿。

If you're referencing another object from the finalizer, you don't know if that object has already been finalized - and how it will behave if it is finalized. (I don't know whether any current CLR runs multiple finalizer threads, but another scary possibility is that it's in the process of being finalized when your finalizer runs.)

所有你应该需要一个终结要做的就是,你自己直接释放非托管资源。如果你引用其他托管类型其自身的资源,让他们做他们的工作中的及其的终结。

All you should need to do in a finalizer is release unmanaged resources that you own directly. If you're referencing other managed types which own resources, let them do their job in their finalizers.

这篇关于为什么"最后确定方法不应该引用任何其他对象"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆