1

I have used Exit Sub within With statement to avoid Set statement in below code. Will this clear the object or is there a way to do this?

Dim FolderPathStr As String
With CreateObject("Scripting.FileSystemObject")
  If .FolderExists(FolderPathStr) = False Then
    MsgBox "Folder does not Exist"
    Exit Sub
  End If
End With
Dhay
  • 585
  • 7
  • 29
  • Don't know what you mean by "clear object", but VBA is garbage-collected and should have no problem with this. – John Coleman Jul 31 '20 at 15:06
  • @JohnColeman - I'm pretty sure (after hearing Mathieu Guindon say it many times, and also [here](https://stackoverflow.com/questions/1775470/forcing-garbage-collection)), that VBA is reference-counted, not garbage-collected. – BigBen Jul 31 '20 at 15:07
  • 1
    @BigBen reference counting is one possible way to manage garbage collection. – John Coleman Jul 31 '20 at 15:08
  • I think we're just talking semantics then lol. – BigBen Jul 31 '20 at 15:08
  • 1
    @BigBen The point is that unlike something like C where you need to explicitly free memory, the VBA runtime does it for you automatically. Like most things with VBA, its approach to doing so is 1990s technology, so it certainly doesn't have a state of the art garbage collection. – John Coleman Jul 31 '20 at 15:11
  • @JohnColeman - yep. Completely agree with that statement, especially the 1990s technology part. – BigBen Jul 31 '20 at 15:11
  • @PaulOgilvie So it means the object created by `CreateObject("Scripting.FileSystemObject")` will be cleared / set to nothing when `Exit Sub` – Dhay Jul 31 '20 at 15:39

1 Answers1

2

As the comments say, VBA has automatic garbage collection. That means that when the VBA run time determines an object or variable is no longer used, it may release the object or variable.

In your example, the Scripting.FileSystemObject object can no longer be accessed once the End With statement is reached so VBA may release the object there.

Would you have assigned the created object to an object variable, then there are two ways that the object can or will be released. Example:

Sub Example

    Dim myObject As Object
    Set myObject = CreateObject("Scripting.FileSystemObject")
    '
    ' ...whatever you want to do with it...
    '
    Set myObject = Nothing    ' explicit release of the object

End Sub                       ' implicit release of the object

The explicit release of the object releases the object by programmer command. That may be usefull of a lot if work still follows where the object is no longer needed as it could decrease the resource demand of the program

The implicit release of the object occurs at the end of the Sub because the object variable ceases to exist - VBA releases the variable and release its object.

Paul Ogilvie
  • 25,048
  • 4
  • 23
  • 41
  • 1
    There in no garbage collection in VBA at all. Destruction of objects is completely deterministic and based on [COM reference count](https://learn.microsoft.com/en-us/windows/win32/com/managing-object-lifetimes-through-reference-counting). – GSerg Jul 31 '20 at 15:55
  • @GSerg, I understand reference counting. The filesystem object may be split into two parts (I do not know that for sure). A "user level" part that maintains the state as the user is using it (e.g. currently set working directory, file specs, etc.), and a system level part. Reference counting could occur on both levels. The user level can be released as I describe above. The system level can be released by reference counting, when there are no client parts connected to the system part anymore. But such a description would go beyond the simple explanation that would help this user. – Paul Ogilvie Jul 31 '20 at 16:05