0

Do we have Lapsed listener problem in java script ?

if we subscribe to an event and then not unsubscribe it will it lead to Lapsed Listener problem.

5
  • stackoverflow.com/questions/12528049/… Commented Jul 18, 2017 at 1:32
  • 3
    What is "Lapsed listener problem"? Commented Jul 18, 2017 at 1:33
  • We can't really help you until you tell us what you think a "lapsed listener" is. If you subscribe to an event on an object, then any code in that event handler is "reachable" and objects referenced in that listener cannot be garbage collected. That is how Javascript garbage collection is designed. It is not so much a "problem" as it is something you have to learn in order to use Javascript properly. If you're done with a listener, remove it from the host object - the rule is no simpler than that. Commented Jul 18, 2017 at 6:00
  • @jfriend00 that answered my question what i meant was if reference to any object still exist in the event listeners . Can it be garbage collected? Commented Jul 22, 2017 at 9:06
  • @user2224055 If the event listeners have a reference to the object they are listening to, that might be a reference cycle, but that doesn't prevent them from being garbage collected as usual. Commented Jul 22, 2017 at 18:15

1 Answer 1

0

Making my comment into an answer since it seemed to have answered the question for you.

If you subscribe to an event on an object, then any code in that event handler is "reachable" and objects referenced in that listener cannot be garbage collected.

That is how Javascript garbage collection is designed. It is not so much a "problem" as it is something you have to learn in order to use Javascript properly. If you're done with a listener and the object you were listening to is still alive (and thus the listener could still be called again), then remove the listener from the host object - the rule is no simpler than that.

Sign up to request clarification or add additional context in comments.

1 Comment

@user2224055 - Does this answer your question? If so, you can indicate that to the community by clicking the green checkmark to the left of the answer and that will also earn you some reputation points.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.