在设置为空之后,委托仍然会调用类!?! [英] Delegate still calls class after set to nothing!?!

查看:44
本文介绍了在设置为空之后,委托仍然会调用类!?!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个坏错误,或者我不理解

somthing。在我看来,这不应该工作:


----------------------------- -------------

班级应该工作

代表Sub goHere()


Sub StartHere ()

Dim DC作为新DeadClass

将myDel调暗为新的goHere(AddressOf DeadClass.Here)

DC = Nothing

GC.Collect

myDel.Invoke

End Sub

结束班级


班级DeadClass

Sub Here

MsgBox"我是如何到达这里的?"

End Sub

End Class

--------------------------------------


我假设因为一个委托实际上只是一个指向内存中某个地方的指针,所以调用它仍会发送

us在那里,但这不是很危险吗?假设堆上的
位置已被覆盖,谁知道

会发生什么,对吧?


评论,请!

解决方案

没有多少使用代理,但是


猜测,当你实例化一个委托myDel时,将

参考DC移除给代表无效。


OHM#


ZorpiedoMan写道:

这是一个坏错误,或者我不理解某事。在我看来,这不应该工作:

--------------------------------- ---------
班级不应该工作
代表Sub goHere()

Sub StartHere()
Dim DC作为新DeadClass
Dim myDel as new goHere(AddressOf DeadClass.Here)
DC = Nothing
GC.Collect
myDel.Invoke
End Sub
End Class
Class DeadClass
Sub Here
MsgBox"我如何到达这里?
End Sub
End Class
---------- ----------------------------

我在假设,因为代表真的只是一个
指向内存中某个地方的指针,调用它仍会发送给我们,但这不是很危险吗?假设堆上的位置已被覆盖,谁知道会发生什么,对吧?

评论,请!



问候 - OHM#在********** @ BTInternet.com


这是正确的,但内存中被称为
的地方应该不再可用......那个'是什么让我担心

担心。


我认为这肯定是因为垃圾收集器在后面运行。


设置为空后使用gc.collect再试一次。

< an ******* @ discussion.microsoft.com> schrieb im Newsbeitrag

news:04 **************************** @ phx.gbl ... < blockquote class =post_quotes>这是正确的,但是被调用的内存中的位置应该不再可用......这就是让我担心的事情。


This is either a bad bug, or I''m not understanding
somthing. In my mind, this should NOT work:

------------------------------------------
Class ShouldntWork
Delegate Sub goHere()

Sub StartHere()
Dim DC as new DeadClass
Dim myDel as New goHere(AddressOf DeadClass.Here)
DC = Nothing
GC.Collect
myDel.Invoke
End Sub
End Class

Class DeadClass
Sub Here
MsgBox "How Did I Get Here?"
End Sub
End Class
--------------------------------------

I''m assuming that since a delegate is really just a
pointer to a place in memory, that invoking it still sends
us there, but isn''t this dangerous? Assume that that
location on the heap has been overwritten, who knows what
will happen, right?

Comments, Please!

解决方案

Havent used delegates much, but

Just guessing, as you instantiate a delegate myDel, the removal of the
reference DC to the delegate has no effect.

OHM#

ZorpiedoMan wrote:

This is either a bad bug, or I''m not understanding
somthing. In my mind, this should NOT work:

------------------------------------------
Class ShouldntWork
Delegate Sub goHere()

Sub StartHere()
Dim DC as new DeadClass
Dim myDel as New goHere(AddressOf DeadClass.Here)
DC = Nothing
GC.Collect
myDel.Invoke
End Sub
End Class

Class DeadClass
Sub Here
MsgBox "How Did I Get Here?"
End Sub
End Class
--------------------------------------

I''m assuming that since a delegate is really just a
pointer to a place in memory, that invoking it still sends
us there, but isn''t this dangerous? Assume that that
location on the heap has been overwritten, who knows what
will happen, right?

Comments, Please!



Regards - OHM# On**********@BTInternet.com


That''s correct, but the place in memory that is being
called should no longer be available... that''s what has me
worried.


I think it''s surely because of the Garbage Collector running in the back.

Use gc.collect after set to nothing and try again.
<an*******@discussions.microsoft.com> schrieb im Newsbeitrag
news:04****************************@phx.gbl...

That''s correct, but the place in memory that is being
called should no longer be available... that''s what has me
worried.



这篇关于在设置为空之后,委托仍然会调用类!?!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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