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.

  • https://stackoverflow.com/questions/12528049/if-a-dom-element-is-removed-are-its-listeners-also-removed-from-memory – epascarello Jul 18 '17 at 01:32
  • 3
    What is "Lapsed listener problem"? – guest271314 Jul 18 '17 at 01: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. – jfriend00 Jul 18 '17 at 06: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? – user2224055 Jul 22 '17 at 09: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. – Bergi Jul 22 '17 at 18:15

1 Answers1

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.

jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • @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. – jfriend00 Aug 18 '17 at 06:16