locked
Need to remove event listeners ever? RRS feed

  • Question

  • I know the former implementation of IE have been infamous for DOM event handler leaks. Now in the sample code for DP0 I only see calls to addEventListener but never a removeEventListener, so I guess removing the DOM node that the listener was attached too will remove it from memory properly now? Maybe IE uses weak references for event handlers now?

    Also I assume adding the same handler over and over to an Element will only add it once?

    What about handlers added to the Application class? Do they need to be removed?

    Saturday, February 11, 2012 4:42 AM

Answers

  • If you add an event listener to a DOM element via addEventListener, this listener should not keep the element in memory, even if it captures a reference to the element in a closure. If you find out that you are seeing leaks due to this, please report it. I can not make this same guarantee with attachEvent.

    WinJS.Application maintains a global queue, so you should remove listeners if they are no longer needed. That is to say the lifetime of the WinJS.Application object is intended to be the lifetime of your application and it will keep around any functions you place into it unless they are explicitly removed.

    Cheers,

    -Jeff

    Tuesday, February 14, 2012 11:01 PM
  • Hi Phil,

    You do not need to remove the event listeners from the application.  They will be cleaned up with the application cleanup.  Similarly with the other DOM elements.

    If you add the same event handler to the same element multiple times, it will only be called once on the event.

    -Jeff


    Jeff Sanders (MSFT)

    Monday, February 13, 2012 7:18 PM
    Moderator

All replies

  • Hi Phil,

    You do not need to remove the event listeners from the application.  They will be cleaned up with the application cleanup.  Similarly with the other DOM elements.

    If you add the same event handler to the same element multiple times, it will only be called once on the event.

    -Jeff


    Jeff Sanders (MSFT)

    Monday, February 13, 2012 7:18 PM
    Moderator
  • If you add an event listener to a DOM element via addEventListener, this listener should not keep the element in memory, even if it captures a reference to the element in a closure. If you find out that you are seeing leaks due to this, please report it. I can not make this same guarantee with attachEvent.

    WinJS.Application maintains a global queue, so you should remove listeners if they are no longer needed. That is to say the lifetime of the WinJS.Application object is intended to be the lifetime of your application and it will keep around any functions you place into it unless they are explicitly removed.

    Cheers,

    -Jeff

    Tuesday, February 14, 2012 11:01 PM