VBA引用计数 - 对象销毁 [英] VBA Reference counting - Object destruction

查看:506
本文介绍了VBA引用计数 - 对象销毁的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近我碰到了一个让我发抖的问题;它让我很忙,我在网上找不到一个透明的解释。

它与Excel对象的破坏有关(我使用所有的时间,永远不会真的

导致我的问题的背景:

使用常规对象,您可以使用关键字SET和NEW来实例化一个对象。
例如:

  Set classInstance = New className 

每当我们以这种方式实例化时,该对象都在堆内存中创建,引用计数器增加1.

如果我不t添加更多的引用,以下语句将引用计数返回到零:

  Set classInstance = Nothing 

当引用计数为0时,对象被破坏并从内存中清除,classInstance指向。



我读过的内容:

当我们使用CREATEOBJECT函数时,它返回对COM对象的引用。

 设置oApp = CreateObject(Excel.Application)

即使我们可以说:

 设置oApp = nothing 

对象的引用计数将变为0,oApp不会再指向对象。



我的问题

1)为什么这种类型的对象需要调用方法.Quit before该对象实际上正在从内存中删除?

添加对需要.close方法的工作簿对象(workbooks.add或workbook.open)的引用也是如此。
为什么在引用计数为零时这些对象不会自动被破坏?

例如我们说的情况是这样的:

  set oRange = nothing 

2)需要说:

  oApp.Quit 
set oApp = nothing

由于Application对象在应用.Quit时已经从内存中清除,所以没有任何对象可以被释放。

唯一的原因我可以想出,为什么oApp将在退出之后设置为Nothing,因为它可能指向未使用的内存位置(在堆上),并且可能会导致混淆,如果这个内存将被重新分配(虽然在VBA中我觉得很难想象)。
如果这个结论是正确的,我在质疑自己,我想从知道答案的人那里收到确认。

请告诉我,如果我看到错误的话。 3)他们在VBA对对象的引用(例如上面的代码中的oApp)中调用它们,我将它们看作C中的指针变量。
可以安全地使用这个陈述或再一次,我看到这个错误吗?



一般不难应用。退出并设置为无,但是很高兴收到一些关于该主题的准确信息。所以我知道100%为什么我这样做。

解决方案

好问题:)



Excel控制其创建对象。



设置 oApp = Nothing 只是破坏对象引用。它不会删除应用程序。要销毁Excel对象,您必须使用它的 .Quit 方法。



无论何时, Set x = Nothing ,将删除名为 x 的引用(指针)到其相关对象。这并不意味着对象本身将被从内存中删除。
对象是否将从内存中删除,取决于各种因素。


  1. 是否有更多的参考指向相同的对象。如果有的话,对象不会被删除。引用计数必须为零。

  2. 该对象的析构函数的内部实现。

.Quit 方法定义为慷慨地删除excel分配的所有内存对象,并关闭自己。



它类似于在VB6中的表单上调用关闭。例如,vb6中的表单。

  Dim f As Form 
设置f = Form1
f 。显示

'
'~~>其余的代码
'

设置f =没有

这会破坏形式吗? :)



FOLLOWUP


问题如何2?谢谢 - Kim Gysen 14分钟前




它可能不完全如这里所示,编译器优化可能会使事情行为不同...但这是基本的正在工作的概念。


Lately I've bumped into a question that made me pounder; it kept me busy and I couldn't find a transparent explanation for it on the net.
It is related to the destruction of Excel objects (which I use all the time and never really questioned before).

Background leading to my question:
With regular objects, you can instantiate an object using the keywords SET and NEW. For example:

Set classInstance = New className

Whenever we instantiate this way, the object is created in the heap memory and the reference counter is increased by 1.
In case I don't add more references, the following statement would bring the reference count back to zero:

Set classInstance = Nothing 

When the reference count goes to 0, the object is destroyed and cleared from memory and the "classInstance" points to .

What I've read:
When we use the "CREATEOBJECT" function, it returns a reference to a COM object.

Set oApp = CreateObject("Excel.Application")

Even though we could say:

Set oApp = nothing 

The objects' reference count will go to 0, and oApp will not point to the object anymore.

My questions:
1) Why is it that this type of object requires to call the method .Quit before the object is actually being removed from memory?
The same goes when adding a reference to a workbook object (workbooks.add or workbook.open) which requires the .close method. Why can't these objects be automatically destroyed when bringing the reference count to zero?
Which is the case when we say for example:

set oRange = nothing 

2) And is there a need to say:

oApp.Quit
set oApp = nothing 

Since the Application object is already cleared from memory when applying .Quit, there is no object to be released anymore.
The only reason I could come up with, why oApp would be set to Nothing after Quit, would be because it could be pointing to an unused memory location (on the heap) and could lead to confusion later if this memory would be re-assigned (although in VBA I find this hard to imagine). I was questioning myself if this conclusion is correct and I would like to receive confirmation for that from someone who knows the answer.
Please, tell me if I see this wrongly.

3) What they call in VBA "a reference to an object" (such as oApp in the code above), I see them as pointer variables in C. Would it be safe to use this statement or again, am I seeing this wrongly?

Generally is not hard to apply .Quit and set to nothing, but it would be nice to receive some accurate information on the topic. So that I know for 100% percent why I am doing it.

解决方案

Good Question :)

Excel controls the creation of its objects. Likewise it also controls their destruction.

Setting oApp = Nothing just destroys the object reference. It doesn't remove the Application. To destroy an Excel object, you have to use it's .Quit method.

Whenever you do, Set x = Nothing, the reference(pointer) named x to its relevant object is removed. This doesn't mean that the object itself will be removed from the memory. Whether the object will be removed from memory or not, depends on various factors.

  1. Whether there are more references pointing towards the same object. If there are, the object will not be removed. The reference count must be zero.
  2. The internal implementation of the destructor of that object.

The .Quit method is defined to graciously remove all the memory objects excel has allocated, and close itself.

It is similar to calling Close on a form in VB6. Take for example, a form in vb6.

Dim f As Form
Set f = Form1
f.Show

'
'~~> Rest of the code
'

Set f = Nothing

Will this destroy the form? :)

FOLLOWUP

How about question 2? Thanks – Kim Gysen 14 mins ago

It might not be exactly as shown here, and compiler optimizations may make things behave differently... but this is the basic concept that is at work.

这篇关于VBA引用计数 - 对象销毁的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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