Q re: memory allocation and deallocation for member objects of a class

I have defined a class, Collection, which stores Objects in an internal Array and provides the standard methods for adding and removing objects from the Collection. My question is: if I have an instance of Collection which contains a number of objects in its internal array, if I set that instance to Nothing, will that deallocate the memory for the objects in the internal array as well, or will doing so result in a memory leak? I.e., if I have the following code:

It depends. VB uses COM objects. COM objects have an internal reference counter which counts the number of references to ithe object. When no variable references an object the object destroys itself.
e.g:

Set objMyObject = New clsMyClass '<<<<1st Created Instance
colMyCollection.Add(objMyObject)

Set objMyObject = New clsMyClass '<<<< 2nd Created Instance
colMyCollection.Add(objMyObject)
'Now colMyCollection has 2 references to 2 different instances of clsMyClass and objMyObject has a reference to the second create instance of
clsMyClass.

set colMyCollection= Nothing
The collection is destroyed and so is the first created instance clsMyClass but the 2nd create instance remains

So, in my original example, setting objCollection = Nothing will, indeed, clear out all the allocated memory if, and only if there are no other references to the objects contained within the collection? Does everyone concur?

As JR2003 pointed out, COM objects behave in this manner. If you are only placing standard VB objects into your collection you should be just fine.

If you are going to place objects from outside of VB into your collection, perhaps from third party libraries or windows API created objects, then you need to be more careful as those objects may need a dispose method called or may need to be destroyed BEFORE you lose your reference to it.

For example, you can use the GDI APIs to do fast drawing. You can create a brush object to paint with using the GetStockObject() API. You could feasibly store the reference to your newly created brush in a collection. To properly deallocate the memory used by that brush however, you must pass that reference to the DeleteObject() API. If you simply set the collection equal to nothing, then you would lose your reference to your brush and would be unable to properly call DeleteObject(), resulting in a memory leak. In this scenario, you would have to loop through the collection and call DeleteObject() for each brush reference in it before releasing the collection.

So again, if you are only placing standard VB objects into your collection then setting your collection equal to nothing is just fine. If you have any unmanaged objects then you need to release them manually before releasing your collection object.

Yes <<<"in my original example, setting objCollection = Nothing will, indeed, clear out all the allocated memory if, and only if there are no
" other references to the objects contained within the collection? Does everyone concur?

If you follow that as a general rule you will always be right unless as Idle_Mind points out you are using something ordinary like GDI functions.

Also worth noting is that if you have a very large number of objects in your collection the operation "Set objCollection = Nothing" can take much longer than iterating through the collection in the reverse order to which the objects were created removing each item individually and then setting the colleciton object to nothing. This though is just a performance consideration.

Featured Post

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

I’ve seen a number of people looking for examples of how to access web services from VB6. I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…

Background
What I'm presenting in this article is the result of 2 conditions in my work area:
We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…

Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code.
This l…