[Greasemonkey] Re: Memory Leaks

Andre gm at andrecgn.de
Wed Nov 9 18:32:22 EST 2005


This EventManager looks great, but I think there might be a bug with 
additional "unload" functions that are added.

Add will actually add "unload" functions to the EventListner queue.

Cleanup will call the unload function (unless it is itself) and then 
remove it from the listener queue.

I think there is a potential, that the function already has been call 
because the event fired.
Add should only add the function to its registry and not to the actual 
queue. Then CleanUp should just call it and not remove it.

Wrong?

btw, nice that it can be used with an ElementId string instead of the 
actual element reference.

Andre

----- Original Message -----
From: John Plsek <jplsek at iinet.net.au>
Sent: Mittwoch, 9. November 2005 16:22:28
Subject: Memory Leaks
> I keep forgetting to turn html forma off!!!!!!
> 
> anyway, yes, event listeners are bad ... mmmkayyy
> 
> I use the following - can't recall where I got it from, but I'm 99.9999% 
> sure the code was in the public domain
> 
>  EventManager= {
>    _registry: null,
>    Initialise: function() {
>      if (this._registry == null) {
>        this._registry = [];
>        EventManager.Add(window, "_unload", this.CleanUp);
>      }
>    },
>    Add: function(obj, type, fn, useCapture) {
>      this.Initialise();
>      var realType=(type=="_unload"?"unload":type);
>      if (typeof obj == "string")
>        obj = document.getElementById(obj);
>      if (obj == null || fn == null)
>        return false;
>      obj.addEventListener(realType, fn, useCapture);
>      this._registry.push({obj:obj, type:type, fn:fn, 
> useCapture:useCapture});
>      return true;
>    },
>    CleanUp: function() {
>      for (var i = 0; i < EventManager._registry.length; i++) {
>        with(EventManager._registry[i]) {
>          if(type=="unload") fn();
>          obj.removeEventListener(type,fn,useCapture);
>        }
>      }
>      EventManager._registry = null;
>    }
>  };
> 
> and, instead of (for example)
> 
> element.addEventListener("click", someFunction, false);
> 
> use
> 
> EventManager.Add(element, "click", someFunction, false);
> 
> 
> J



More information about the Greasemonkey mailing list