Skip to content

Commit 377667f

Browse files
committed
fix(socket) prevent memory leak
1 parent 1c02086 commit 377667f

File tree

1 file changed

+13
-8
lines changed

1 file changed

+13
-8
lines changed

lib/socket.js

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@
22
'use strict';
33

44
var path = require('path'),
5-
findit = require('findit'),
65

7-
assert = require('assert'),
6+
findit = require('findit'),
87
io = require('socket.io'),
98

109
Socket,
@@ -35,7 +34,8 @@
3534

3635
function onConnection(socket, onMsg, callback) {
3736
var msg, onDisconnect, onMessage,
38-
onFind = function(conNum, data) {
37+
indexEmpty = Clients.indexOf(null),
38+
onFind = function(conNum, data) {
3939
var dir, name,
4040
d = data;
4141

@@ -55,10 +55,10 @@
5555
onMsg = null;
5656
}
5757

58-
assert(socket, 'socket could not be empty!');
59-
assert(callback, 'callback could not be empty!');
60-
61-
++ConNum;
58+
if (indexEmpty >= 0)
59+
ConNum = indexEmpty;
60+
else
61+
ConNum = Clients.length;
6262

6363
if (!Clients[ConNum]) {
6464
msg = log(ConNum, 'find connected');
@@ -73,7 +73,12 @@
7373
onFind(conNum, command);
7474
}.bind(null, ConNum),
7575
onDisconnect = function(conNum) {
76-
Clients[conNum] = null;
76+
if (Clients.length !== conNum + 1) {
77+
Clients[conNum] = null;
78+
} else {
79+
Clients.pop();
80+
--ConNum;
81+
}
7782

7883
log(conNum, 'find disconnected');
7984

0 commit comments

Comments
 (0)