title: VBA Reference counting - Object destruction
body: <p>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.<br>
  It is related to the destruction of Excel objects (which I use all the time and never <em>really</em> questioned before). </p>
  
  <p>Background leading to my question:<br>
  With regular objects, you can instantiate an object using the keywords SET and NEW. 
  For example: </p>
  
  <pre><code>Set classInstance = New className
  </code></pre>
  
  <p>Whenever we instantiate this way, the object is created in the heap memory and the reference counter is increased by 1.<br>
  In case I don't add more references, the following statement would bring the reference count back to zero: </p>
  
  <pre><code>Set classInstance = Nothing 
  </code></pre>
  
  <p>When the reference count goes to 0, the object is destroyed and cleared from memory and the "classInstance" points to . </p>
  
  <p>What I've read:<br>
  When we use the "CREATEOBJECT" function, it returns a reference to a COM object. </p>
  
  <pre><code>Set oApp = CreateObject("Excel.Application")
  </code></pre>
  
  <p>Even though we could say: </p>
  
  <pre><code>Set oApp = nothing 
  </code></pre>
  
  <p>The objects' reference count will go to 0, and oApp will not point to the object anymore. </p>
  
  <p><strong>My questions</strong>:<br>
  1) Why is it that this type of object requires to call the method .Quit before the object is actually being removed from memory?<br>
  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?<br>
  Which is the case when we say for example: </p>
  
  <pre><code>set oRange = nothing 
  </code></pre>
  
  <p>2) And is there a need to say:  </p>
  
  <pre><code>oApp.Quit
  set oApp = nothing 
  </code></pre>
  
  <p>Since the Application object is already cleared from memory when applying .Quit, there is no object to be released anymore.<br>
  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.<br>
  Please, tell me if I see this wrongly. </p>
  
  <p>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? </p>
  
  <p>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. </p>