[Greasemonkey] Re: Memory Leaks

Andre gm at andrecgn.de
Wed Nov 9 15:46:20 EST 2005


wow, thanks!!! I should have known there is an unload event. I was 
actually searching for the list of events that you (or somebody here on 
the list) had sent around a couple of weeks ago but it was under some 
other heading.

My list is static, so it is easy.

Thanks, again,
Andre

----- Original Message -----
From: Jeremy Dunck <jdunck at gmail.com>
Sent: Mittwoch, 9. November 2005 15:36:14
Subject: Memory Leaks
> On 11/9/05, Andre <gm at andrecgn.de> wrote:
> 
>>Hi,
>>
>>you are right. I add several event listeners for a newly created button.
>>It is a div with:
>>
>>     var trigger = document.createElement("div");
>>     trigger.addEventListener("mouseover", eh_onmouseover, false);
>>     trigger.addEventListener("mouseout", eh_onmouseout, false);
>>     trigger.addEventListener("click", eh_onclick, false);
>>
>>How would I remove them? The refresh is done by the page, not by my code.
> 
> 
> Assuming the handlers are closures over content, this is the bug in moz:
> https://bugzilla.mozilla.org/show_bug.cgi?id=241518
> 
> Note that it doesn't block 1.5, but it is being actively worked.
> 
> As for how to unhook them, assuming your list is static, do something like this:
> 
> body.addEventListener("unload", unhooker, false);
> 
> function unhook() {
>   //get a ref to your hooked elm;
>   trigger.removeEventListener("mouseover", eh_onmouseover, false);
>   trigger.removeEventListener("mouseout", eh_onmouseout, false);
>   trigger.removeEventListener("click", eh_onclick, false);
> }
> 
> If you hooked list is dynamic, stuff gets more complicated.  You'll
> need to keep your own list list of all event hooks.
> 
> var hookers = {}
> 
> function myAddEventListener(obj,ev,handler,cap) {
>   obj.addEventListener(ev, handler, cap);
>   hookers.append( { obj:obj,ev:ev,handler:handler,cap:cap} );
> }
> 
> //use that instead of obj.addEventListener.
> 
> then define unhook like:
> function unhook() {
>   for (hooker in hookers) {
>     hooker.obj.removeEventListener(hooker.ev, hooker.handler, hooker.cap);
>   }
> }
> 
> and hook body.unload as before.



More information about the Greasemonkey mailing list