Weak References with Dictionaries in Actionscript
Posted by Zachary Pinter on October 11, 2007 at 08:02 PM
Actionscript dictionaries are a great tool for dealing with memory management because you can use them to create weak references to objects. This means that the object automatically goes away upon garbage collection if the only reference to it is in the Dictinary.
To use a dictionary in this manner, simply pass true to the constructor. However, one should note that this only makes the keys of the Dictionary weak.
For example:
1 class MyObject { 2 public var message:String; 3 } 4 5 var dict:Dictionary = new Dictionary(true); 6 var obj1:MyObject = new MyObject(); 7 obj1.message = "My really long message..."; 8 var obj2:MyObject = new MyObject(); 9 obj2.message = "My second really long message..."; 10 11 dict[obj1] = true; //obj1 is weakly referenced 12 dict[2] = obj2; //obj2 is strongly referenced
If the only reference to an object is the key of a weakly referenced dictionary, the object will not persist after garbage collection. However, if the only reference to an object is the value of a key in a weakly referenced dictionary, the reference is considered strong and will force the object to persist after garbage collection.
So, what do you do if you want to store a dictionary of weakly referenced objects, but key them off of a meaningful value (such as an item id) rather than their reference id?
The answer is to use a simple WeakReference class (source):
1 class WeakRef { 2 private var dic:Dictionary; 3 4 public function WeakRef( obj:* ) { 5 dic = new Dictionary( true ); 6 dic[obj] = 1; 7 } 8 9 public function get():* { 10 for( var item:* in dic ) { 11 return item; 12 } 13 return null; 14 } 15 }
From here, you can do the following:
1 var dict:Dictionary = new Dictionary(true); 2 var obj1:MyObject = new MyObject(); 3 obj1.message = "My really long message..."; 4 var obj2:MyObject = new MyObject(); 5 obj2.message = "My second really long message..."; 6 7 dict[1] = new WeakReference(obj1); //weakly referenced value 8 dict[2] = new WeakReference(obj2); //weakly referenced value
Now, you can key based off of a meaningful value, and still get all the benefits of weak references.
Actionscript Hash
Posted by Zachary Pinter on October 09, 2007 at 08:36 PM
On the surface, the Dictionary object in Actionscript seems quite limited compared to the Hash and HashMap libraries of other languages. However, the functionality you would expect is still there, it's just obscure.
For example, to loop through all keys in a Dictionary:
1 for (var key:Object in groupMap) 2 { 3 trace(key + ", " + groupMap[key]); 4 }
To loop through all values in a Dictionary:
1 for each (var item:Object in groupMap) 2 { 3 trace(item); 4 }
To check if a key exists (in a way that's distinguishes from a key whose value could be null):
1 obj['key'] === undefined //key exists
To remove a key/value pair from a Dictionary:
1 delete obj['key']
All of the above code will also work for a regular object treated like a hash.