Browse Source

timers: fix memory leak in setTimeout

Closing handle is leaked when setTimeout called with arguments which are
1. a callback
2. zero delay
(i.e. setTimeout(function(){}, 0); )
v0.7.4-release
Yoshihiro Kikuchi 13 years ago
committed by Ben Noordhuis
parent
commit
f2f30286bf
  1. 5
      lib/timers.js
  2. 3
      test/simple/test-timers-zero-timeout.js

5
lib/timers.js

@ -151,7 +151,10 @@ exports.setTimeout = function(callback, after) {
timer = new Timer(); timer = new Timer();
if (arguments.length <= 2) { if (arguments.length <= 2) {
timer._onTimeout = callback; timer._onTimeout = function() {
callback();
timer.close();
}
} else { } else {
var args = Array.prototype.slice.call(arguments, 2); var args = Array.prototype.slice.call(arguments, 2);
timer._onTimeout = function() { timer._onTimeout = function() {

3
test/simple/test-timers-zero-timeout.js

@ -27,6 +27,7 @@ var assert = require('assert');
var ncalled = 0; var ncalled = 0;
setTimeout(f, 0, 'foo', 'bar', 'baz'); setTimeout(f, 0, 'foo', 'bar', 'baz');
var timer = setTimeout(function(){}, 0);
function f(a, b, c) { function f(a, b, c) {
assert.equal(a, 'foo'); assert.equal(a, 'foo');
@ -37,6 +38,8 @@ var assert = require('assert');
process.on('exit', function() { process.on('exit', function() {
assert.equal(ncalled, 1); assert.equal(ncalled, 1);
// timer should be already closed
assert.equal(timer.close(), -1);
}); });
})(); })();

Loading…
Cancel
Save