2012-08-14 07:51:48 -07:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Memory leak hunter. Walks a tree of objects looking for DOM nodes.
|
|
|
|
* Usage:
|
|
|
|
* leakHunt({
|
|
|
|
* thing: thing,
|
|
|
|
* otherthing: otherthing
|
|
|
|
* });
|
|
|
|
*/
|
2012-11-30 00:07:59 -08:00
|
|
|
function leakHunt(root) {
|
|
|
|
var path = [];
|
|
|
|
var seen = [];
|
2012-08-14 07:51:48 -07:00
|
|
|
|
|
|
|
try {
|
2012-11-30 00:07:59 -08:00
|
|
|
var output = leakHunt.inner(root, path, seen);
|
2012-08-14 07:51:48 -07:00
|
|
|
output.forEach(function(line) {
|
|
|
|
dump(line + '\n');
|
|
|
|
});
|
|
|
|
}
|
|
|
|
catch (ex) {
|
|
|
|
dump(ex + '\n');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-30 00:07:59 -08:00
|
|
|
leakHunt.inner = function LH_inner(root, path, seen) {
|
2012-08-14 07:51:48 -07:00
|
|
|
var prefix = new Array(path.length).join(' ');
|
|
|
|
|
|
|
|
var reply = [];
|
|
|
|
function log(msg) {
|
|
|
|
reply.push(msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
var direct
|
|
|
|
try {
|
|
|
|
direct = Object.keys(root);
|
|
|
|
}
|
|
|
|
catch (ex) {
|
|
|
|
log(prefix + ' Error enumerating: ' + ex);
|
|
|
|
return reply;
|
|
|
|
}
|
|
|
|
|
2012-11-30 00:07:59 -08:00
|
|
|
try {
|
|
|
|
var index = 0;
|
|
|
|
for (var data of root) {
|
|
|
|
var prop = '' + index;
|
|
|
|
leakHunt.digProperty(prop, data, path, seen, direct, log);
|
|
|
|
index++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (ex) { /* Ignore things that are not enumerable */ }
|
2012-08-14 07:51:48 -07:00
|
|
|
|
2012-11-30 00:07:59 -08:00
|
|
|
for (var prop in root) {
|
2012-08-14 07:51:48 -07:00
|
|
|
var data;
|
|
|
|
try {
|
|
|
|
data = root[prop];
|
|
|
|
}
|
|
|
|
catch (ex) {
|
2012-11-30 00:07:59 -08:00
|
|
|
log(prefix + ' ' + prop + ' = Error: ' + ex.toString().substring(0, 30));
|
2012-08-14 07:51:48 -07:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2012-11-30 00:07:59 -08:00
|
|
|
leakHunt.digProperty(prop, data, path, seen, direct, log);
|
|
|
|
}
|
2012-08-14 07:51:48 -07:00
|
|
|
|
2012-11-30 00:07:59 -08:00
|
|
|
return reply;
|
|
|
|
}
|
2012-08-14 07:51:48 -07:00
|
|
|
|
2012-11-30 00:07:59 -08:00
|
|
|
leakHunt.hide = [ /^string$/, /^number$/, /^boolean$/, /^null/, /^undefined/ ];
|
2012-08-14 07:51:48 -07:00
|
|
|
|
2012-11-30 00:07:59 -08:00
|
|
|
leakHunt.noRecurse = [
|
|
|
|
/^string$/, /^number$/, /^boolean$/, /^null/, /^undefined/,
|
|
|
|
/^Window$/, /^Document$/,
|
|
|
|
/^XULDocument$/, /^XULElement$/,
|
|
|
|
/^DOMWindow$/, /^HTMLDocument$/, /^HTML.*Element$/, /^ChromeWindow$/
|
|
|
|
];
|
2012-08-14 07:51:48 -07:00
|
|
|
|
2012-11-30 00:07:59 -08:00
|
|
|
leakHunt.digProperty = function LH_digProperty(prop, data, path, seen, direct, log) {
|
|
|
|
var newPath = path.slice();
|
|
|
|
newPath.push(prop);
|
|
|
|
var prefix = new Array(newPath.length).join(' ');
|
|
|
|
|
|
|
|
var recurse = true;
|
|
|
|
var message = leakHunt.getType(data);
|
|
|
|
|
|
|
|
if (leakHunt.matchesAnyPattern(message, leakHunt.hide)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (message === 'function' && direct.indexOf(prop) == -1) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (message === 'string') {
|
|
|
|
var extra = data.length > 10 ? data.substring(0, 9) + '_' : data;
|
|
|
|
message += ' "' + extra.replace(/\n/g, "|") + '"';
|
|
|
|
recurse = false;
|
|
|
|
}
|
|
|
|
else if (leakHunt.matchesAnyPattern(message, leakHunt.noRecurse)) {
|
|
|
|
message += ' (no recurse)'
|
|
|
|
recurse = false;
|
|
|
|
}
|
|
|
|
else if (seen.indexOf(data) !== -1) {
|
|
|
|
message += ' (already seen)';
|
|
|
|
recurse = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (recurse) {
|
|
|
|
seen.push(data);
|
|
|
|
var lines = leakHunt.inner(data, newPath, seen);
|
|
|
|
if (lines.length == 0) {
|
|
|
|
if (message !== 'function') {
|
|
|
|
log(prefix + prop + ' = ' + message + ' { }');
|
2012-08-14 07:51:48 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2012-11-30 00:07:59 -08:00
|
|
|
log(prefix + prop + ' = ' + message + ' {');
|
|
|
|
lines.forEach(function(line) {
|
|
|
|
log(line);
|
|
|
|
});
|
|
|
|
log(prefix + '}');
|
2012-08-14 07:51:48 -07:00
|
|
|
}
|
|
|
|
}
|
2012-11-30 00:07:59 -08:00
|
|
|
else {
|
|
|
|
log(prefix + prop + ' = ' + message);
|
|
|
|
}
|
|
|
|
};
|
2012-08-14 07:51:48 -07:00
|
|
|
|
2012-11-30 00:07:59 -08:00
|
|
|
leakHunt.matchesAnyPattern = function LH_matchesAnyPattern(str, patterns) {
|
2012-08-14 07:51:48 -07:00
|
|
|
var match = false;
|
|
|
|
patterns.forEach(function(pattern) {
|
|
|
|
if (str.match(pattern)) {
|
|
|
|
match = true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return match;
|
2012-11-30 00:07:59 -08:00
|
|
|
};
|
2012-08-14 07:51:48 -07:00
|
|
|
|
2012-11-30 00:07:59 -08:00
|
|
|
leakHunt.getType = function LH_getType(data) {
|
2012-08-14 07:51:48 -07:00
|
|
|
if (data === null) {
|
|
|
|
return 'null';
|
|
|
|
}
|
|
|
|
if (data === undefined) {
|
|
|
|
return 'undefined';
|
|
|
|
}
|
|
|
|
|
|
|
|
var type = typeof data;
|
|
|
|
if (type === 'object' || type === 'Object') {
|
2012-11-30 00:07:59 -08:00
|
|
|
type = leakHunt.getCtorName(data);
|
2012-08-14 07:51:48 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return type;
|
2012-11-30 00:07:59 -08:00
|
|
|
};
|
2012-08-14 07:51:48 -07:00
|
|
|
|
2012-11-30 00:07:59 -08:00
|
|
|
leakHunt.getCtorName = function LH_getCtorName(aObj) {
|
2012-08-14 07:51:48 -07:00
|
|
|
try {
|
|
|
|
if (aObj.constructor && aObj.constructor.name) {
|
|
|
|
return aObj.constructor.name;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (ex) {
|
|
|
|
return 'UnknownObject';
|
|
|
|
}
|
|
|
|
|
|
|
|
// If that fails, use Objects toString which sometimes gives something
|
|
|
|
// better than 'Object', and at least defaults to Object if nothing better
|
|
|
|
return Object.prototype.toString.call(aObj).slice(8, -1);
|
2012-11-30 00:07:59 -08:00
|
|
|
};
|