Skip to content
Browse files

fixing collection.find memory leaks

  • Loading branch information...
1 parent 092f4f2 commit 99c7fdced301964f560f6108b24777db28631981 @outbounder outbounder committed Feb 21, 2011
Showing with 63 additions and 24 deletions.
  1. +2 −0 .gitignore
  2. +1 −1 index.js
  3. +50 −17 lib/mongodb/cursor.js
  4. +10 −5 lib/mongodb/db.js
  5. +0 −1 lib/mongodb/index.js
View
2 .gitignore
@@ -1,3 +1,5 @@
.DS_Store
*.swp
*.seed
+.project
+.settings
View
2 index.js
@@ -1 +1 @@
-module.exports = require('./lib/mongodb');
+module.exports = require('./lib/mongodb');
View
67 lib/mongodb/cursor.js
@@ -120,7 +120,13 @@ Cursor.prototype.toArray = function(callback) {
items.push(item);
} else {
callback(err, items);
+
+ // XXX prevents memory leaks
+ items = null;
}
+
+ // XXX prevents memory leaks
+ item = null;
});
} else {
callback(new Error("Cursor is closed"), null);
@@ -162,6 +168,9 @@ Cursor.prototype.each = function(callback) {
self.state = Cursor.CLOSED;
callback(err, null);
}
+
+ // XXX prevents memory leaks
+ item = null;
});
});
} else {
@@ -429,20 +438,27 @@ Cursor.prototype.nextObject = function(callback) {
if(self.state == Cursor.INIT) {
try {
- self.db.executeCommand(self.generateQueryCommand(), function(err, result) {
- if(!err && result.documents[0] && result.documents[0]['$err']) {
- self.close(function() {callback(result.documents[0]['$err'], null);});
- return;
- }
- self.queryRun = true;
- self.state = Cursor.OPEN; // Adjust the state of the cursor
- self.cursorId = result.cursorId;
- self.totalNumberOfRecords = result.numberReturned;
-
- // Add the new documents to the list of items
- self.items = self.items.concat(result.documents);
- self.nextObject(callback);
- });
+ // XXX prevents memory leaking
+ var commandHandler = function(err, result) {
+ if(!err && result.documents[0] && result.documents[0]['$err']) {
+ self.close(function() {callback(result.documents[0]['$err'], null);});
+ return;
+ }
+ self.queryRun = true;
+ self.state = Cursor.OPEN; // Adjust the state of the cursor
+ self.cursorId = result.cursorId;
+ self.totalNumberOfRecords = result.numberReturned;
+
+ // Add the new documents to the list of items
+ self.items = self.items.concat(result.documents);
+ self.nextObject(callback);
+
+ // XXX prevent memory leaks
+ result = null;
+ };
+
+ self.db.executeCommand(self.generateQueryCommand(), commandHandler);
+ commandHandler = null;
} catch(err) {
callback(new Error(err.toString()), null);
}
@@ -498,11 +514,21 @@ Cursor.prototype.getMore = function(callback) {
} else {
self.close(function() {callback(null, null);});
}
+
+ // XXX prevents memory leaks
+ result = null;
});
+
+ // XXX prevents memory leaks
+ getMoreCommand = null;
+
} catch(err) {
- self.close(function() {
+ // XXX prevents memory leaks
+ var handleClose = function() {
callback(new Error(err.toString()), null);
- });
+ };
+ self.close(handleClose);
+ handleClose = null;
}
}
@@ -596,13 +622,20 @@ Cursor.prototype.close = function(callback) {
if(this.cursorId instanceof self.db.bson_serializer.Long && this.cursorId.greaterThan(self.db.bson_serializer.Long.fromInt(0))) {
try {
var command = new KillCursorCommand(this.db, [this.cursorId]);
- this.db.executeCommand(command, function(err, result) {});
+ var killCusrosCommandHandler = function(err, result) {};
+ this.db.executeCommand(command, killCursorComamndHandler);
+
+ // XXX prevents memory leaks
+ killCursorComamndHandler = null;
} catch(err) {}
}
this.cursorId = self.db.bson_serializer.Long.fromInt(0);
this.state = Cursor.CLOSED;
if (callback) callback(null, this);
+
+ // XXX prevent memory leaks
+ this.items = null;
};
/**
View
15 lib/mongodb/db.js
@@ -538,15 +538,20 @@ Db.prototype.executeCommand = function(db_command, callback) {
}
// Correctly handle serialization errors
- var msg = db_command.toBinary()
-
+ var msg = db_command.toBinary();
+ var checkMasterHandler = function(err, reply, dbinstance){
+ dbinstance.serverConfig.masterConnection.send(db_command);
+ };
+
try{
this.serverConfig.masterConnection.send(msg);
} catch(err){
- this.checkMaster_(this, function(err, reply, dbinstance){
- dbinstance.serverConfig.masterConnection.send(db_command);
- });
+ this.checkMaster_(this, checkMasterHandler);
}
+
+ // XXX prevents memory leaks
+ db_command = null;
+ checkMasterHandler = null;
};
/**
View
1 lib/mongodb/index.js
@@ -1,5 +1,4 @@
var sys = require('sys')
-
// // Add both the BSON Pure classes and the native ones
var BSONPure = exports.BSONPure = require('./bson/bson');
var BSONNative = null

1 comment on commit 99c7fdc

@Emmerman

You can remove killCusrosCommandHandler (coz it's useless)
Use just this.db.executeCommand(command);

Please sign in to comment.
Something went wrong with that request. Please try again.