-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
On 12/15/10 10:14 AM, Asen Bozhilov wrote:
> Peter Foti :
>
>> function addEvent( obj, type, fn ) {
>> if ( obj.attachEvent ) {
>> obj['e'+type+fn] = fn;
>> obj[type+fn] = function(){obj['e'+type+fn]( window.event );}
>> obj.attachEvent( 'on'+type, obj[type+fn] );
>> }
>
> This is perfectly example of how you can setup circular reference
> pattern, which cause memory leak in older IE.
>
> Variable Object of addEvent -> obj -> obj[type+fn] ->
> function[[Scope]] -> Variable Object of addEvent
>
> You cannot prevent this leak. The only one unreliable way is to setup
> unload handler which breaks this circular reference.
>
There is a perfectly valid second way to prevent this leak: an event
dispatcher that is referenced through the global scope:
https://gist.github.com/742453
I've only implemented IE's attachEvent and detachEvent, but the basics
are there. A few notes:
* "eventData" MUST be referenced through the global scope. If you close
around it, it will leak.
* An unload handler is not needed because no circular references are
created except through the global scope (DOM_Node.onevent ->
function_object.[[scope]] -> scope_chain -> GlobalObject.eventData ->
eventData.Object -> Object.element -> DOM_Node).
* The dispatcher function returned from createDispatcher never closes
around the element, so it is never put in its activation object and a
circular reference is only formed through the global scope.
I'm currently finishing up a blog post on this very topic and I will
post the link to this group once I complete it, but the bottom line is
this is the technique that toolkits such as Dojo use to keep
dojo.connect from leaking.
- --
Bryan Forbes
http://www.reigndropsfall.net
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.11 (Darwin)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/
iEYEARECAAYFAk0JGJkACgkQOIgX4HDKDz3pnQCfSadrxWBf/1M//O6Ey1T3fM2K
U+kAn0WDSaELHtzXrcuYmHp8XR+oS5rp
=f9+L
-----END PGP SIGNATURE-----
--
To view archived discussions from the original JSMentors Mailman list:
http://www.mail-archive.com/[email protected]/
To search via a non-Google archive, visit here:
http://www.mail-archive.com/[email protected]/
To unsubscribe from this group, send email to
[email protected]