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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Running code when a reference count is decremented

Status
Not open for further replies.

scorpio66

Programmer
Jun 27, 2001
229
GB
Hi

Does anyone know how to run code in a VB6 class that is executed when an object is set to Nothing (i.e. when the reference count is decremented)

Class_Terminate is run when the object is destroyed (i.e. reference count is equal to 0) but this is not the case when the object is in a hierarcy with a reference to a parent object.

Any clues? At the moment I am handling this by having a dispose method which must be called prior to setting the property to Nothing but I'd rather have this code run automatically as it can leave objects hanging around in memory if the dispose method is not called.

Chaz
 
Yep, you're right it will leave objects hanging round in memory. And quite frankly; your's is the only possible solution.

But not exactly in the scenario you posted:
You say that the object doesn't get terminated because it has a reference to a parent object. This is not the case, it probably doesn't get destroyed because the parent object has a reference to the object as well (so actually the reference count doesn't reach zero by setting the object to nothing in the client).

It does not mather as to how many references the object to be destroyed has to other objects (these will automatically be reset to zero), the only thing that mathers is that other objects have references to the object itself. And in your case (parent/child) it might be so that the child has a refence to the parent and the parent has one to the child. This is something you need to be very careful with, as these are the so called "circular references" where you can't destroy both of the objects by setting them to nothing, since they still hold references to each other. Dispose is the only way to "kill" those references and have the objects be destroyed.
Greetings,
Rick
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top