I have a reference to an object. This object has a timer event with a weak reference. Example:
timer.addEventListener(TimerEvent.TIMER, timerHandler, false, 0, true);
Now I remove this reference (test is the variable containing the reference):
test = null;
And yet, the timerHandler keeps being fired. Is this impossible, so that I must have somekind of mistake in my code, without any other possibility?
Or is this indeed not supposed to stop the timer function from being run all the time?
The garbage collector doesn't operate continually, so it's likely that it just hasn't run yet. When it finally does, your handler should stop being called. If not, there is probably another reference to it.
When I run the example below, I see
timer
traced indefinitely, even thoughhandler
has been set to null and theEventDispatcher
has a weak reference. However, if I force the garbage collector to run by uncommenting theSystem.gc()
line (using the debug player), the handler is never called.In general, you shouldn't rely on the garbage collector for the correct operation of your program.