.NET对象的构造 [英] Destructon of .NET objects

查看:89
本文介绍了.NET对象的构造的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




为什么当终结器被调用时,运行时将对象设置为null?这不会阻止大量的错误吗?


我刚刚调用了obj .__ dtor();从我制作的MC ++包装器类及其

仍然引用该类型甚至是destructed。为什么我必须在我的视图中设置

它为null它应该由破坏设置。


????

解决方案

<<。>>写道:

为什么运行时没有在调用终结器时将对象设置为null?这不会阻止大量的错误吗?




它究竟将它设置为null?您是否希望它通过整个托管堆(和堆栈)遍历

来查找对象的所有引用?
对象?如果有对象的引用,为什么它首先被确定为?b $ b?大概是因为引用它的对象

也有资格进行垃圾收集,或者因为它没有知道数据的类型,在这种情况下它不会不知道

无论如何都设置为null。


请注意,没有将对象设置为null这样的事情。 -

只有将reference-type变量的值设置为null。


-

Jon Skeet - < sk *** @ pobox.com>
http:// www .pobox.com / ~siget

如果回复小组,请不要给我发邮件




" Jon Skeet [C#MVP]" < SK *** @ pobox.com>在消息中写道

新闻:MP ************************ @ msnews.microsoft.c om ...

<<。>>写道:

为什么运行时没有在终结器调用
时将对象设置为null?这不会阻止大量的错误吗?
它究竟会将它设置为null?您是否希望它遍历整个托管堆(和堆栈)以查找对象的所有引用?如果有对象的引用,为什么它首先被最终确定?大概是因为引用它的对象也有资格进行垃圾收集,或者因为它不知道数据的类型,在这种情况下它不会知道
无论如何将它设置为null。




如果它不知道将它设置为null的位置,那么它如何知道什么和

其中对象引用位于第一个位置。

当然它可以将引用设置为null,它知道它在哪里,它是什么
它被破坏了,简单的事情将该引用设置为null。

请注意,没有将对象设置为null这样的事情。 -
只有将reference-type变量的值设置为null。


多数民众赞成我的意思。 - Jon Skeet - < sk *** @ pobox.com>
http://www.pobox.com/~skeet
如果回复小组,请不要给我发邮件


>如果它不知道将它设置为null的位置,那么它如何知道什么和

其中对象引用位于第一个位置。
当然它可以将引用设置为null,其中知道它在哪里,什么是
并且它被破坏了,简单的设置引用为null。




正如乔恩所说,为什么它是当你仍然提到

时最终确定了吗?如果它是活的,它就不会被垃圾收集。


它知道对象的位置,而不是它的引用。 IIRC垃圾

收藏家的工作方式是确定哪些物品是活的,然后移除

其他(根据定义,它们不是活的)所以它不会知道*哪里*

他们被引用,除非它触及*一切* :)我想这是

Jon暗示的是什么?


n!


Hi,

Why doesnt the runtime set the object to null when the finalizer has been
called? Wouldnt this prevent a slew of bugs?

I just called the obj.__dtor(); from a MC++ wrapper class I made and its
still referencing the type even tho its "destructed". Why do I have to set
it to null in my view it should be set by destruction.

????

解决方案

<<.>> wrote:

Why doesnt the runtime set the object to null when the finalizer has been
called? Wouldnt this prevent a slew of bugs?



Where exactly would it set it to null? Would you want it to walk
through the entire managed heap (and stack) to find all references to
the object? If there are references to the object, why has it been
finalized in the first place? Presumably either because the objects
referring to it are eligible for garbage collection too, or because it
doesn''t know the type of the data, in which case it wouldn''t know to
set that to null anyway.

Note that there''s no such thing as "setting an object to null" -
there''s only setting the value of a reference-type variable to null.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too



"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...

<<.>> wrote:

Why doesnt the runtime set the object to null when the finalizer has been called? Wouldnt this prevent a slew of bugs?
Where exactly would it set it to null? Would you want it to walk
through the entire managed heap (and stack) to find all references to
the object? If there are references to the object, why has it been
finalized in the first place? Presumably either because the objects
referring to it are eligible for garbage collection too, or because it
doesn''t know the type of the data, in which case it wouldn''t know to
set that to null anyway.



If it doesnt know where to set it to null, then how does it know what and
where the object reference is in the first palce.
Ofcourse it can set the reference to null, its knows where it is, what it is
and its been destructed, simple matter of setting that reference to null.
Note that there''s no such thing as "setting an object to null" -
there''s only setting the value of a reference-type variable to null.

Thats what I mean. --
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too



> If it doesnt know where to set it to null, then how does it know what and

where the object reference is in the first palce.
Ofcourse it can set the reference to null, its knows where it is, what it is and its been destructed, simple matter of setting that reference to null.



As Jon said, why is it being finalized when you still have a reference to
it? It wouldn''t be garbage collected if it was ''live''.

It knows where the object is, but not its references. IIRC the garbage
collector works by determining which objects are live, and then removing the
others (as, by definition, they are not live) so it wouldn''t know *where*
they were being referenced unless it touched *everything* :) I think this is
what Jon was implying?

n!


这篇关于.NET对象的构造的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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