views:

542

answers:

3

In a VB6 application, I have a Dictionary whose keys are Strings and values are instances of a custom class. If I call RemoveAll() on the Dictionary, will it first free the custom objects? Or do I explicitly need to do this myself?

Dim d as Scripting.Dictionary

d("a") = New clsCustom
d("b") = New clsCustom

' Are these two lines necessary?
Set d("a") = Nothing
Set d("b") = Nothing

d.RemoveAll
+2  A: 

Yes, all objects in the Dictionary will be released after a call to RemoveAll(). From a performance (as in speed) standpoint I would say those lines setting the variables to Nothing are unnecessary, because the code has to first look them up based on the key names whereas RemoveAll() will enumerate and release everything in one loop.

Neil C. Obremski
+1  A: 

RemoveAll will remove all the associations from the Dictionary: both the keys and values. It would be a reference leak for the Dictionary to keep a reference to the values in the Dictionary.

Chris Smith
+1  A: 

If there are no other variables that reference the items in the collection then those objects should be handed to the Garbage Collector to be cleaned up the next time the GC is run.

If you, for example do this where sObj is a static variable somewhere then the when the GC is invoked next by the system, the first object will be cleaned up but the second which still is referenced by sObj will not.

d("a") = New clsCustom
d("b") = New clsCustom code.
sObj = d("b")

d.RemoveAll()
palehorse