Is Unhooking event handlers needed to avoid memory leaks in IE

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 memory leaks be cause by not detaching event handlers when the user leaves a page?

Answers:

Answer

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.

Answer

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)

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.