Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

Commit

Permalink
Revert "events: don't delete the listeners array in removeListener()"
Browse files Browse the repository at this point in the history
This reverts commit 928ea56.

Keeping the original Array instance in-place essentially causes a memory leak
on EventEmitters that use an infinite number of event names (an incrementing
counter, for example), which isn't an unreasonable thing to want to do.

Fixes #3702.
  • Loading branch information
TooTallNate committed Jul 12, 2012
1 parent 3a6314d commit 713b924
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 9 deletions.
2 changes: 2 additions & 0 deletions lib/events.js
Expand Up @@ -214,6 +214,8 @@ EventEmitter.prototype.removeListener = function(type, listener) {

if (position < 0) return this;
list.splice(position, 1);
if (list.length == 0)
delete this._events[type];
} else if (list === listener ||
(list.listener && list.listener === listener))
{
Expand Down
9 changes: 0 additions & 9 deletions test/simple/test-event-emitter-remove-listeners.js
Expand Up @@ -42,15 +42,10 @@ function listener3() {
}

var e1 = new events.EventEmitter();
var e1listeners = e1.listeners('hello');
e1.on('hello', listener1);
assert.equal(e1listeners.length, 1);
e1.removeListener('hello', listener1);
assert.deepEqual([], e1.listeners('hello'));

// identity check, listeners array should be the same
assert.equal(e1listeners, e1.listeners('hello'));

var e2 = new events.EventEmitter();
e2.on('hello', listener1);
e2.removeListener('hello', listener2);
Expand All @@ -59,12 +54,8 @@ assert.deepEqual([listener1], e2.listeners('hello'));
var e3 = new events.EventEmitter();
e3.on('hello', listener1);
e3.on('hello', listener2);
var e3listeners = e3.listeners('hello');
assert.equal(e3listeners.length, 2)
e3.removeListener('hello', listener1);
assert.equal(e3listeners.length, 1)
assert.deepEqual([listener2], e3.listeners('hello'));

assert.equal(e3listeners, e3.listeners('hello'));


0 comments on commit 713b924

Please sign in to comment.