Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

A question about "Nothing"

Status
Not open for further replies.

Retsacnal

Programmer
Jan 24, 2002
5
0
0
AU
Hi,

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?

Thanks,

Ryan.
 
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.
Compare Code (Text)
Generate Sort in VB or VBScript
 
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

 
hey retsacnal,

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.

john
 
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
Compare Code (Text)
Generate Sort in VB or VBScript
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top