I’m trying to set up a test that will tell me whether a variable exists in memory or not. I’m running into the problem of my nested function preserving the local variable it uses, called “shouldBeDead”. Here’s my best effort, which doesn’t work for me because the “shouldBeDead” variable is still alive:
addEventListener(Event.ENTER_FRAME, isDeadYet);
function isDeadYet ($):void {
var shouldBeDead = "not dead";
if (!stage.hasEventListener(KeyboardEvent.KEY_DOWN))
stage.addEventListener(KeyboardEvent.KEY_DOWN, test);
function test($):void {
trace("variable is " + shouldBeDead); // outputs: "variable is not dead"
}
}
Is there a way to test if something exists in memory?
Your
Stringcannot be garbaged collected as there is no new instance created but the value is taken from the constant pool that take care ofString,Number,int,etc..If you create a new
Class,Object,Array, etc… these can be garbaged collected and you can trace them with a simple method : putting your instance as a weak key into a dictionnary.So when garbaged collect occured the key will be deleted from the dictionnary.
Here an example of code for testing, and there live code : http://wonderfl.net/c/uP5T :