I have a Dictionary where I hold data for movieclips, and I want the data to be garbage collected if I stop using the movieclips. I'm using the weak keys parameters, and it works perfectly with other data, however I've run into a problem.
This code works great:
var mc = new MovieClip();
var dic = new Dictionary(true);
dic[mc] = 12;
mc = null;
System.gc();
System.gc();
for (var obj in dic)
trace(obj); //this doesn't execute
But when I actually use the movieclip, it stops working:
var mc = new MovieClip();
var dic = new Dictionary(true);
dic[mc] = 12;
addChild(mc);
removeChild(mc);
mc = null;
System.gc();
System.gc();
for (var obj in dic)
trace(obj); //this prints [object Movieclip]
Why does this happen? Is it something I'm doing wrong? Is there a workaround?
Edit: I know that for this specific example I can use delete dic[mc]
, but of course this is a simplified case. In general, I don't want to manually have to remove the movieclip from the dictionary, but it should be automatic when I don't reference it anymore in the rest of the application.
Edit2: I tried testing what Aaron said, and came up with just weird stuff... just iterating the dictionary (without doing anything) changes the behaviour:
var mc = new MovieClip();
var dic = new Dictionary(true);
dic[mc] = 12;
addChild(mc);
removeChild(mc);
mc = null;
for (var objeto in dic) {} // <-- try commenting out this line
addEventListener('enterFrame', f); // I print the contents every frame, to see if
// it gets removed after awhile
function f(evento)
{
System.gc();
System.gc();
for (var objeto in dic)
trace(objeto);
}
This keeps printing [object Movieclip] every frame, unless I comment out the indicated line, where it doesn't print anything.