我何时必须将变量设置为"Nothing"(无)?在VB6中? [英] When must I set a variable to "Nothing" in VB6?

查看:103
本文介绍了我何时必须将变量设置为"Nothing"(无)?在VB6中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的VB6表单之一中,我创建了其他几个Form对象并将其存储在成员变量中.

In one of my VB6 forms, I create several other Form objects and store them in member variables.

Private m_frm1 as MyForm
Private m_frm2 as MyForm

// Later...
Set m_frm1 = New MyForm
Set m_frm2 = New MyForm

我注意到,每当创建(销毁)此(父)表单时,我就会泄漏内存.我是否有必要将这些成员变量分配给Form_Unload()中的Nothing?

I notice that I'm leaking memory whenever this (parent) form is created and destroyed. Is it necessary for me to assign these member variables to Nothing in Form_Unload()?

通常,什么时候需要?

已解决:当我在有问题的表单上执行Unload时,而不是在将表单设置为Nothing时,解决了此特定的内存泄漏问题.通过将类模块的某些实例也显式设置为Nothing,我设法消除了一些其他的内存泄漏.

SOLVED: This particular memory leak was fixed when I did an Unload on the forms in question, not when I set the form to Nothing. I managed to remove a few other memory leaks by explicitly setting some instances of Class Modules to Nothing, as well.

推荐答案

@Matt Dillard-将这些设置为无"是否可以解决您的内存泄漏?

@Matt Dillard - Did setting these to nothing fix your memory leak?

VB6没有正式的垃圾收集器,更像@Konrad Rudolph所说的.

VB6 doesn't have a formal garbage collector, more along the lines of what @Konrad Rudolph said.

实际上,在您的表单上调用unload似乎是确保清理主表单以及确保每个子表单清理其行为的最佳方法.

Actually calling unload on your forms seems to me to be the best way to ensure that the main form is cleaned up and that each subform cleans up their actions.

我用一个空白项目和两个空白表格进行了测试.

I tested this with a blank project and two blank forms.

Private Sub Form_Load()
  Dim frm As Form2
  Set frm = New Form2
  frm.Show
  Set frm = Nothing
End Sub

运行后,两种形式均可见.将frm设置为空无一事...

After running both forms are left visible. setting frm to nothing did well... nothing.

将frtign设置为空后,唯一打开此表单的句柄是通过引用.

After settign frm to nothing, the only handle open to this form is via the reference.

Unload Forms(1)

我能正确看到问题吗?

  • 乔什

这篇关于我何时必须将变量设置为"Nothing"(无)?在VB6中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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