VB.Net中集合中对象的生命周期 [英] Lifetime of objects in a collection in VB.Net

查看:242
本文介绍了VB.Net中集合中对象的生命周期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找出tmpTabPages在下面的代码位的生命周期。假设表单有一个名为MyTabControl的空TabControl,有一个名为NameCollection的字符串集合。

I'm trying to figure out the lifetime of the tmpTabPages in the following bit of code. Lets assume the form has an empty TabControl named MyTabControl, that there's a collection of strings called NameCollection.

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    For Each itm In NameCollection
        Dim tmpTabPage as New TabPage(itm.toString)

        'Add Controls to tmpTabPage

        MyTabControl.TabPages.Add(tmpTabPage)
    Next
End Sub

由于tmpTabPage的范围是For / Next块,通常它的生命周期将直到块的结束为止?但是,因为它被添加到一个范围之外的集合,它获得与集合相同的生命周期,或在这种情况下的MyTabControl?最后,如果我调用MyTabControl.TabPages.Clear,集合中的tmpTabPages将被销毁或者它们只是占用内存?

Since the scope of the tmpTabPage is the For/Next block, typically it's lifetime would be until the end of the block right? But since it is added to a collection that has a scope outside of the block does it get the same lifetime as the collection, or in this case the MyTabControl? Finally, if I call MyTabControl.TabPages.Clear will the tmpTabPages in the collection be destroyed or will they just sit around taking up memory?

推荐答案

关于从Control(包括TabPage)派生的类的大事是Dispose()方法。它们不受自动垃圾收集的影响,Winforms保留了一个内部表,它将控件的句柄映射到控件引用。这就是为什么,你的主要形式不会突然收到垃圾收集,即使你的程序没有保存引用。

The big deal about classes derived from Control (including TabPage) is the Dispose() method. They are immune from automatic garbage collection, Winforms keeps an internal table that maps the Handle of a control to the control reference. That's why, say, your main form doesn't suddenly get garbage collected, even though your program doesn't keep a reference to it.

将TabPage添加到TabControl的收集照顾自动处置。这同样适用于TabControl,它将被添加到窗体的Controls集合。正常的事件链是你的程序或用户关闭窗体。 Form类重复其子控件并调用其Dispose()方法。 TabControl在其Dispose()方法中执行相同的操作,处理选项卡页。 Windows窗口在此过程中被销毁,从映射表中删除Handle,现在允许垃圾收集器最终收集控件的托管包装。

Adding the TabPage to the TabControl's collection takes care of automatic disposal. The same applies for the TabControl, it would be added to the form's Controls collection. The normal chain of events is that either your program or the user closes the form. The Form class iterates its child controls and calls their Dispose() method. TabControl does the same thing in its Dispose() method, disposing the tab pages. The Windows window gets destroyed in the process, removing the Handle from that mapping table and now allowing the garbage collector to, eventually, collect the managed wrapper for the controls.

是一个讨厌的陷阱,得到许多Winforms程序员的麻烦。如果你删除一个控件从其父的集合,那么你有责任自己处理它。删除它不会自动处理它。 Winforms通过临时将控件重命名为名为停车窗口的隐藏窗口来保持本机窗口的活动。好的功能,它允许你移动控件从一个父母到另一个,而不必销毁和重新创建控件。

There is a nasty trap that gets many Winforms programmers in trouble. If you remove a control from its parent's collection then you get the responsibility of disposing it yourself. Removing it does not automatically dispose it. Winforms keeps the native window alive by temporarily re-parenting the control to a hidden window named the "parking window". Nice feature, it allows you to move a control from one parent to another without having to destroy and re-create the control.

但关键字有临时。这只是暂时的,如果你下一个控制权。所以它从停车窗口移动到新的父母。如果你不实际reparent,那么它将永远活在永远在停车窗口。冻结资源,直到程序终止。这也被称为泄漏。当您已经创建了其他窗口时,如果Windows已拒绝创建另一个窗口,则可能导致程序崩溃。

But the keyword there is "temporarily". It is only temporarily if you next reparent the control. So it gets moved from the parking window to the new parent. If you don't actually reparent it then it will stay alive for ever on the parking window. Gobbling up resources until the program terminates. This is otherwise known as a leak. It can crash your program when Windows refuses to create another window when you've already created 10,000 of them.

ControlCollection.Clear()方法在此处特别有害。它处置控件,他们都被移动到该停车窗口。如果这不是想要的,很少是,你必须自己调用Dispose()。

The ControlCollection.Clear() method is especially harmful here. It does not dispose the controls, they all get moved to that parking window. If that's not intended, it rarely is, you'll have to call Dispose() on them yourself.

这篇关于VB.Net中集合中对象的生命周期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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