【问题标题】:In VB/C# .NET, does a dialog always have to be disposed of manually?在 VB/C# .NET 中,是否总是必须手动处理对话框?
【发布时间】:2012-08-27 09:28:51
【问题描述】:

我正在考虑处置资源,并在不同的处理方式上搞混了。

我刚刚发现,在使用 ShowDialog() 显示的表单上使用 Close() 只是实际上隐藏了它,并没有完全杀死它,可以这么说。虽然这对我目前想要的东西很有用,但我现在担心其他地方的内存泄漏。

使用 ShowDialog() 后,我应该总是在表单上调用 Dispose() 还是使用 Using 块?有区别吗?或者当退出创建子程序时表单会被自动处理吗?例如,我典型的简单用法之一:

Private Sub btnEdit_Click(sender As System.Object, e As System.EventArgs) Handles btnEdit.Click
Dim frm As New frmSomething()
frm.ShowDialog()

'frm is exited by clicking the X or using Close()
'At this point, frm is still in memory.  Is it automatically disposed of
'after the End Sub here, or should I do frm.Dispose() ?

End Sub

【问题讨论】:

    标签: vb.net garbage-collection dispose


    【解决方案1】:

    它不会被自动处理,不会。它很可能不会导致问题,并且很可能有一个终结器来完成所需的一切,因此成本只是终结器运行之前的一些额外资源(以及最终 GC 之前的更长延迟)但最好明确地处理它 - 最好使用 Using 声明:

    Using frm As New frmSomething()
        frm.ShowDialog()
    End Using
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-07-01
      • 2022-09-29
      • 1970-01-01
      • 1970-01-01
      • 2019-02-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多