I'm currently researching why our nodejs app is leaking memory. I'm not a very skilled nodejs programmer (I usually work with C#) so I'm trying to read as much documentation as I can on this subject to find cause and solution.
This question and mostly this blogpost have taught me about the dangers of closures. Our code is full of closures so I'm trying to analyze whether the situation mentioned in these two links apply.
And then there is the habit in many javascript code to write "var self = this;" when using closures, as for example explained here. A "feature" called lexical closure. This is also used abundantly in our code.
So my question is, if you use this lexical closure technique but you don't set self to null at the end of your function, are you leaking self/this?
To give a concrete example, does this function cause a leak:
Achievement.INSERT_QUERY = "SOME QUERY";
Achievement.prototype.insert = function(child, achievement){
var self = this;
var data = [child, achievement];
return this._query(Achievement.INSERT_QUERY, data, function insertResultHandler(err, result){
if(err) {
self.emit(DB.ERROR, err);
return false;
}
self.emit(DB.UPDATED, data);
});
}
Many thanks for any clarification!