I would like to know if setting a reference to a Collection object to Nothing also sets each object in the collection to Nothing, or should I set each object to Nothing myself and then set the Collection reference to Nothing?
When you set an object reference of any object to Nothing, the reference count is reduce by 1 and if the reference count becomes 0 then the object is terminated and all contained objects are "supoosed" to be set to Nothing by VB but it can't hurt to do it yourself in the Class_Terminate event. Remember that an object is not terminated (released) until ALL references to it have been set Nothing. It is possible to have circular references and have objects that never terminte.
I heard that setting the reference to a collection = nothing sets the contents equal to nothing as well. I haven't tested it out myself. I also heard that it is faster to loop through the collection and set all the objects individualy equal to nothing, then set the collection itself equal to nothing. Troy Williams B.Eng.
fenris@hotmail.com
while setting a object to nothing SHOULD release all dependent objects as well, it can't hurt to explicitly dispose of objects, and may save you from hard to find logical bugs in the future.
another thing that i've found is that vb6 is probably the fastest prototyping language on the planet but not everything works as advertised. so i follow a friend's advice: i dimension an object, write the code to destroy it, and insert the important bits in between. that way i know nothing is possibly left hanging.
Remember that setting an object reference to Nothing, implicitly or explicitly does not by itself "destroy" the object. "Destruction" only occurs when the reference count reaches 0. In the following simple example, the collection is not destroyed.
Function ReturnCollection() As Collection
Dim col As New Collection ' Ref Count = 1
Set ReturnCollection = col ' Ref Count = 2
Set col = Nothing ' Ref Count = 1
End Function
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.