Skip to main content
November 27, 2007
Question

It's a Listener's Life

  • November 27, 2007
  • 7 replies
  • 598 views
What happens to a listener registered on a non-display object that is declared within a function? Does it live forever? Does it make the object outlive the function containing it by virtue of the 'event.target' reference to it? Is it necessary then, to employ: 'event.target.removeEventListener(...)' within that listener itself in order to clean things up?

Many thanks for any advice!

(and appologies for possibly missing something fundamental and asking a trivial question.. I'm still low on that learning curve...)
This topic has been closed for replies.

7 replies

kglad
Community Expert
Community Expert
November 28, 2007
you're welcome.
kglad
Community Expert
Community Expert
November 28, 2007
1. nulling an object that has a listener does not ready the object for gc. you must remove the listener.

the way a weak reference is supposed to work, is that listeners added to an object using a weak reference should not need to be removed to ready the object for gc. but that doesn't work: even listeners that use weak references must be removed to ready the object for gc.
November 28, 2007
It was my understanding (base on Moock's Essential AS 3.0) that weak references are all about avoiding unreferenced listeners from becoming stranded in some object's listener list without any means to remove them. That it is all about the garbage collection of the listener, rather than the object it was registered with. But of course, as long as the listener lives, there is going to be a reference to its object, and so it too cannot be gc'd.

Thanks for the input though
kglad
Community Expert
Community Expert
November 28, 2007
1. no

2. you can't. remove the listener first, then null the loader.
November 28, 2007
"no", I am assuming, answers: "This is the whole point of weak references, is it not?..."

Would you care to explain yourself? I'd like to better my understanding; this is the whole point of this communication, is it not?...

kglad
Community Expert
Community Expert
November 27, 2007
1. load an external swf that contains a looping trace statement.

2. attach a listener with a weak reference to the loader.

3. null the loader.

4. (try to) force gc by adding assets

the trace continues.

5. remove the listener.

the trace terminates shortly thereafter.

weak references don't work in the dictionary class, either.
November 28, 2007
hmmm... I don't have much programming experience, but thinking about it logically, it appears to me that this test is flawed for two reasons:

1. If you annul the object with which the listener was registered, it matters not whether the reference to it is weak or strong. In both cases there will be no (countable) references left to the listener (it would be in no object's 'listener list') and thus it should be eligible for garbage collection. The true test is whether the listener will be gc'd without annulling the object. This is the whole point of weak references, is it not?...

2. Once the listening object is annulled, how do you remove the listener? How do you reference it?

I ran a test with a Timer listener for the functionality of weak references. It turns out that whether or not the listening object still exists, and whether or not the reference to the listener is weak or strong, the listener does not get garbage collected. Of course, one could argue that the test program simply did not consume a sufficient amount of memory to trigger the garbage collector, but trying to do that (with a loop, say, that feeds an array with a thousand Text Fields per second), will sooner crash the program than exhibit any evidence of the garbage collection of the listener!

At this point I just cannot think of a way to reliably test the functionality of a weak reference in AS. And so I am simply going to have to ignore the useWeakReference feature, remove listeners manually and hope that the garbage collector knows what it's doing. Pity...
Inspiring
November 27, 2007
kglad,

> unfortunately, weak references don't work with flash.

Really? That's interesting to me. Can you give a brief example? I
haven't done anything in AS3 yet that would take an obvious hit from memory
leaks.


David Stiller
Adobe Community Expert
Dev blog, http://www.quip.net/blog/
"Luck is the residue of good design."


kglad
Community Expert
Community Expert
November 27, 2007
unfortunately, weak references don't work with flash.
kglad
Community Expert
Community Expert
November 27, 2007
the listener exists until it is removed and the object listening cannot be gc'd until the listener is removed (and various other steps are taken).
November 27, 2007
And so, in this particular case, I am assuming that it would be correct to remove the listener by using 'event.target.removeEventListener(...)', as no other reference to the original object with which the listener was registered exists. event.target=null doesn't work.

Thanks kglad