Solved

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

Posted on 2004-09-06
5
244 Views
Last Modified: 2010-05-02
Hi everyone,

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:

Dim objCollection As Collection
Set objCollection = New Collection
// Note: just assume the objects being added below have been allocated previously
objCollection.Add( objObject1 )
objCollection.Add( objObject2 )
objCollection.Add( objObject3 )
Set objCollection = Nothing

Will this correctly deallocate the memory for objObject1 - 3, or will that memory forever be allocated and now unreachable?

Regards,
William W. Lin
wlin@cobius.com
0
Comment
Question by:billin
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 18

Accepted Solution

by:
JR2003 earned 250 total points
ID: 11991825
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:

Dim objMyObject as Object
Dim objMyObject2 as Object
Dim colMyCollection as Collection

Set colMyCollection = New Collection

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

Set objMyObject = Nothing '<<<<< All memory is now cleared
0
 

Author Comment

by:billin
ID: 11991929
Hi JR2003,

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?

Regards,
William W. Lin
wlin@cobius.com
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 11992126
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.

Regards,

Idle_Mind
0
 
LVL 18

Expert Comment

by:JR2003
ID: 11992647
billin,

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.

JR
0
 

Author Comment

by:billin
ID: 11992685
Great - that answers my quetsion. Thanks, JR and Idle_Mind!

Cheers,
William W. Lin
wlin@cobius.com
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question