|
metadata
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)
|
|
|
metadata
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.
|
|
|
metadata
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.
|
|
|
metadata
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?
|
|
|
metadata
just remove it yourself to be safe…
|
|
|
metadata
I agree with jonathan, because there is no reason not to remove it.
|
|
|
metadata
> *Originally posted by **[Maqrkk](/forums/4/topics/84213?page=1#posts-1870164):***
>
> I agree with jonathan, because there is no reason not to remove it.
Maybe I want to reuse it later?
|
|
|
metadata
You have to add it again when you re-add your object anyway, when it’s garbage collected.. ?
|
|
|
metadata
|
|
|
metadata
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.
|
|
|
metadata
> *Originally posted by **[Wordblind](/forums/4/topics/84213?page=1#posts-1870500):***
>
> 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!
|
|
|
metadata
learn to ask proper questions instead of having us guess at what you mean then!
|
|
|
metadata
> *Originally posted by **[jonathanasdf](/forums/4/topics/84213?page=1#posts-1870790):***
>
> learn to ask proper questions instead of having us guess at what you mean then!> *Originally posted by **[UnknownGuardian](/forums/4/topics/84213?page=1#posts-1869859):***
>
> 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?
|