[Greasemonkey] Re: GM & FF memory leaks?

Andre gm at andrecgn.de
Thu Feb 9 00:04:54 EST 2006


I only found the EventManager somewhere as an example and fixed one bug.

The idea is that any code you want to execute triggered by an event is 
registered using e.g.

	EventManager.Add(trigger, "click",  myOnclick,  false);

The Eventmanager keeps track of them all and registers its own unload 
event. Before the next page gets loaded or this page gets refreshed, an 
'unload' event is triggered and the Eventmanager.CleanUp routine removes 
all Listeners by calling removeEventListener. Supposedly this should 
help against the memory leaks. I am not sure whether it really helps.

Jeremy Dunck wrote:
> 
> EventManager is designed singleton-ish, so no need to instantiate it.
> 
> You do need to initialize it, though.
> Include his code, then call this:
> EventManager.Initialise();


Actually it initializes itself on the first call:
	  Add: function(obj, type, fn, useCapture) {
	    this.Initialise();
	    ...

> 
> Then use it as he described:
> EventManager.Add(trigger, "click",  myOnclick,  false);
> 
> But you'll still want to clean up on unload:
> window.addEventListener('unload', function(){EventManager.CleanUp();}, false);

Not really. On the first call (in Initialise()) it registers an 'unload' 
EventListern with
	EventManager.Add(window, "_unload", this.CleanUp);

> 
> .... He's got a special case for unload vs. _unload; I'm not sure what
> that's about.

'_unload' is its own unload event listener that really gets registered. 
All the others from user code are named 'unload' and will be stored in 
the EventManger registry only in order to be called in 'CleanUp()'. They 
will not be registered in an addEventListener call, because there would 
be no guarantee, that they are called exactly once. In the original code 
they might get called twice.

Andre



More information about the Greasemonkey mailing list