将对象设置为“Nothing"有什么好处? [英] What are the benefits of setting objects to "Nothing"

查看:38
本文介绍了将对象设置为“Nothing"有什么好处?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到 Stack Overflow 社区的一些成员将在关闭过程中使用 Set Object = Nothing.我能够找到为什么这对 Access 实例有用,但是在为 Excel 执行此操作时没有令人满意的答案,所以我的问题是在 VBA 中将对象设置为 Nothing 有什么好处?

I've noticed that some members of the Stack Overflow community will use Set Object = Nothing in closing procedures. I was able to find why this is useful for instances of Access, but no answer has been satisfying when it comes to doing this for Excel, so my question is What are the benefits of setting objects to Nothing in VBA?

在下面的示例代码中,将我的对象 wsTest 设置为等于 Nothing 是否浪费空间?否则,如果这样做实际上是好的做法,为什么?

In the sample code below, is setting my objects ws and Test equal to Nothing a waste of space? Else, if doing so is in fact good practice, why?

Dim ws as Worksheet
Dim Test as Range

Set ws = Sheets("Sheet1")
Set Test = ws.Range("A1")

    'Utilize Test variable (copy, paste, etc)

Set Test = Nothing
Set ws = Nothing

Exit Sub 

推荐答案

如果这是托管的 .NET 代码(垃圾收集),您必须发布代码>您曾经访问过的每个 COM 对象,以免宿主进程 (EXCEL.EXE) 可能会继续在后台运行,消耗内存并且无法完全拆除.

If this was managed .NET code (which is garbage-collected), you'd have to Release every single COM object you ever accessed, lest the host process (EXCEL.EXE) would likely remain running in the background, consuming memory and unable to completely tear down.

但这是 VBA 代码(引用计数),而且 VBA 代码使用宿主应用程序控制的对象 - 当宿主应用程序关闭时,这些对象将死亡,当发生这种情况时VBA 执行上下文早已不复存在.

But this is VBA code (which is reference-counted), moreover VBA code that uses objects that the host application controls - these objects will die when the host application shuts down, and when that happens the VBA execution context is long gone.

换句话说,所有这些 Set ... = Nothing 指令都是完全多余的.

In other words, all these Set ... = Nothing instructions are completely redundant.

在某些特定情况下,当您处理第 3 方 API/类型库时,对象可能没有完全清理干净.例如,您可能正在创建一个 Access.Application 实例,并发现 Excel 退出后任务管理器中的幽灵"ACCESS.EXE 进程仍然保持打开状态:这表明​​您正在泄漏对象以某种方式在某处引用,而 Set ... = Nothing 可以帮助防止这种情况发生.

In some specific cases, when you're dealing with a 3rd-party API / type library, it's possible that objects don't entirely clean up. For example you might be creating an Access.Application instance, and find that a "ghost" ACCESS.EXE process remains open in Task Manager well after Excel exited: that's a sign that you're leaking an object reference somehow, somewhere, and Set ... = Nothing can help prevent that.

但是,我不建议像这样系统地清空所有对象引用.仅当这样做会导致问题.即便如此,也将是一两个对象将所有东西拖下水,而不是所有.如果 ACCESS.EXE 正常关闭,就没有理由用这些指令来弄乱您的代码.

However I wouldn't recommend systematically nulling all object references like that. Only when not doing it causes a problem. And even then, it's going to be one or two objects dragging everything down, not all of them. If ACCESS.EXE shuts down properly, there's no reason to clutter up your code with such instructions.

避免在全局状态中存储对象引用也有帮助.如果一切都是本地的,理论上所有涉及的对象都会在本地作用域一退出就被销毁.

Avoiding storing object references in global state helps, too. If everything is local, in theory all objects involved are destroyed as soon as the local scope exits.

这篇关于将对象设置为“Nothing"有什么好处?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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