Does Scripting.Dictionary's RemoveAll() method release all of its elements first?

Deadly 提交于 2020-01-02 13:47:59

问题


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

回答1:


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.




回答2:


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.




回答3:


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()


来源:https://stackoverflow.com/questions/75011/does-scripting-dictionarys-removeall-method-release-all-of-its-elements-first

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!