Is Unhooking event handlers needed to avoid memory leaks in IE

后端 未结 2 1050
执笔经年
执笔经年 2021-01-16 12:12

I am having some memory leak problems in IE.

I have been reading, that not unhooking event handlers in IE, could cause memory leaks. Is this true?
Could my memo

相关标签:
2条回答
  • 2021-01-16 12:54

    Some of the older browsers have had issues in regards to this. When there's registered event handler, consider this;

    1) in script engine a callback registry is kept, an event is bound here with 2 things, a DOMNode and a function pointer

    2) DOMNode may 'dissapear' - and same thing could 'happen' to the function (less likely though).

    code like <a onclick="a = (a?a+1:0);" id="getme">... results in an anonumous function which you will reference by var anchor = document.getElementById('getme'); anchor.onclick' To perfectly purge it one must delete anchor.onclick in addition to detach eventlistener.

    Some people may wrant at this code but it will certainly wipe the anchors onclick away

    var a=document.getElementById('getme')
    window.detachEvent("onclick", a.onclick);
    delete a.onclick;
    a.parentNode.removeChild(a)
    
    0 讨论(0)
  • 2021-01-16 12:57

    Douglas Crockford has an excellent post on JSscript memory leaks (JScript is IE's implementation of JavaScript/ECMAScript).

    It basically comes down to this: IE has separate memory management (and thus garbage collection) for the DOM and for JScript. Because of this, IE has trouble cleaning up the cycling references between DOM objects and event handlers.

    The way to work around this is to make sure that you always remove the event handlers from the DOM objects (or set them to null) before you throw away the DOM object.

    0 讨论(0)
提交回复
热议问题