[Greasemonkey] Memory Leaks

John Plsek jplsek at iinet.net.au
Thu Nov 10 02:22:28 EST 2005


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