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 = Nothingjust destroys the object reference. It doesn’t remove the Application. To destroy an Excel object, you have to use it’s.Quitmethod.Whenever you do,
Set x = Nothing, the reference(pointer) namedxto 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.
The
.Quitmethod is defined to graciously remove all the memory objects excel has allocated, and close itself.It is similar to calling
Closeon a form in VB6. Take for example, a form in vb6.Will this destroy the form? 🙂
FOLLOWUP
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.