From 713b9249e1e7cbc028e7e4f29ef7dd6a68e03375 Mon Sep 17 00:00:00 2001 From: Nathan Rajlich Date: Thu, 12 Jul 2012 15:40:45 -0700 Subject: [PATCH] Revert "events: don't delete the listeners array in removeListener()" This reverts commit 928ea564d16da47e615ddac627e0b4d4a40d8196. 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. --- lib/events.js | 2 ++ test/simple/test-event-emitter-remove-listeners.js | 9 --------- 2 files changed, 2 insertions(+), 9 deletions(-) diff --git a/lib/events.js b/lib/events.js index 7e219a14a4..c2b604f892 100644 --- a/lib/events.js +++ b/lib/events.js @@ -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)) { diff --git a/test/simple/test-event-emitter-remove-listeners.js b/test/simple/test-event-emitter-remove-listeners.js index 33ee3df564..70526668c4 100644 --- a/test/simple/test-event-emitter-remove-listeners.js +++ b/test/simple/test-event-emitter-remove-listeners.js @@ -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); @@ -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'));