Tshepo Mgaga
Tshepo Mgaga

Reputation: 272

Mootools event leak

The example to demonstrate the issue can be found here:

link text

The test shows a basic Mootools class that contains an element variable with a click event attached. There’s also a “cleanup” function to remove the event and nullify the element variable.

My problem is that when I loop a thousand times to create the “LeakClass” instance and clean it up, it causes a major memory leak like there’s no tomorrow. I tested this on IE8 and Chrome.

On the other hand what I’ve noticed is that if I comment out the line that adds the “click” event, the code doesn’t leak.

Can somebody please help me structure the class/event in a manner that it doesn’t leak.

Thanks in advance.

Upvotes: 1

Views: 413

Answers (1)

Dimitar Christoff
Dimitar Christoff

Reputation: 26165

what are you trying to achieve?

this.element = null; does not detatch the element from the DOM or destroy it, it simply removes your reference to it. to do so you should use this.element.destroy(); which calls up internal cleaning functions - first applies a little GC of child nodes, then does .empty(), then removes it from the DOM and finally removes it altogether. your code will just create multitudes of elements that will stop being referenced but lack of reference does not mean lack of existence. each element will use up memory on its own, events or not.

Upvotes: 1

Related Questions