UnknownGuardian
8138 posts
|
Okay, I’ve heard of all this talk about if you don’t remove your event listeners when you remove your object they will sit there in memory and eat it up. (Yum).
But does that only apply to event listeners that are attached to the stage? Or will they be removed anyway if they are attached to the object when the garbage collector eats the object?
Attached means this.addEventListener(XXX) or stage.addEventListener(XXX)
|
|
|
Draco18s
6860 posts
|
ANY event listener provides a reference to the object it is attached to, so those objects won’t be garbage collected unless you use weak listeners.
|
|
|
Moonkey
1007 posts
|
Event listeners attached to this won’t actually keep the object in memory.
Think about what happens when you do
a.addEventListener("some event", b.someFunction);
‘a’ gets a reference to ‘b’ so that it can call b.someFunction when required.
If a and b are the same thing, adding that event listener won’t effect its garbage collection any more than var c = this.someFunction will.
You’ll only run into problems when ‘a’ is going to be kept around in memory longer than ‘b’ is. The stage is a good example since it never gets garbage collected, so any references given to it by adding event listeners will keep b in memory indefinitely.
On the other hand, if you have a menu class which calls button1.addEventListener(MouseEvent.CLICK, startGame), that’s not a problem.
button1 (a) will never need to be garbage collected until the menu (b) is also ready to be garbage collected.
When the menu goes out of scope, button1 will also go out of scope (assuming there were no external references created) and both will be free for garbage collection.
|
|
|
UnknownGuardian
8138 posts
|
So, Moonkey, is this event listener is garbage collected:
//class stuff
addEventListener(MouseEvent.ROLL_OVER, sideOver);
//other stuff
parent.removeChild(this);
//no outside references to this class
Then the roll over event is no longer going to be in memory, since it was garbage collected?
|
|
|
jonathanasdf
3910 posts
|
just remove it yourself to be safe…
|
|
|
Maqrkk
45 posts
|
I agree with jonathan, because there is no reason not to remove it.
|
|
|
UnknownGuardian
8138 posts
|
Originally posted by Maqrkk:
I agree with jonathan, because there is no reason not to remove it.
Maybe I want to reuse it later?
|
|
|
Maqrkk
45 posts
|
You have to add it again when you re-add your object anyway, when it’s garbage collected.. ?
|
|
|
jonathanasdf
3910 posts
|
|
|
|
Wordblind
1052 posts
|
I think by “it”, UG meant the DisplayObject. If he just removes the ROLL_OVER listener during REMOVED_FROM_STAGE, then he would need to replace it on ADDED_TO_STAGE, meaning that there would still be a listener attached. Fortunately, he can just leave it like it is, and it will be collected. There are ways around this, but sometimes it is just easier to actually understand how the system works.
|
|
|
UnknownGuardian
8138 posts
|
Originally posted by Wordblind:
I think by “it”, UG meant the DisplayObject. If he just removes the ROLL_OVER listener during REMOVED_FROM_STAGE, then he would need to replace it on ADDED_TO_STAGE, meaning that there would still be a listener attached. Fortunately, he can just leave it like it is, and it will be collected. There are ways around this, but sometimes it is just easier to actually understand how the system works.
Finally an answer that I wanted! Thanks a bunch!
|
|
|
jonathanasdf
3910 posts
|
learn to ask proper questions instead of having us guess at what you mean then!
|
|
|
UnknownGuardian
8138 posts
|
Originally posted by jonathanasdf:
learn to ask proper questions instead of having us guess at what you mean then!
Originally posted by UnknownGuardian:
So, Moonkey, is this event listener is garbage collected:
//class stuff
addEventListener(MouseEvent.ROLL_OVER, sideOver);
//other stuff
parent.removeChild(this);
//no outside references to this class
Then the roll over event is no longer going to be in memory, since it was garbage collected?
|