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

Commit

Permalink
events: don't delete the listeners array in removeListener()
Browse files Browse the repository at this point in the history
  • Loading branch information
TooTallNate committed Mar 16, 2012
1 parent 761a82b commit 928ea56
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
2 changes: 0 additions & 2 deletions lib/events.js
Expand Up @@ -186,8 +186,6 @@ 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: 9 additions & 0 deletions test/simple/test-event-emitter-remove-listeners.js
Expand Up @@ -42,10 +42,15 @@ 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 @@ -54,8 +59,12 @@ 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'));


1 comment on commit 928ea56

@quartzo
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems that this commit creates a memory leak: mongodb-native uses a incremental reference for every request, and this._events keeps all the older references as a zero length array.

Please sign in to comment.