1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
|
/* 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/. */
"use strict";
/**
* Memory leak hunter. Walks a tree of objects looking for DOM nodes.
* Usage:
* leakHunt({
* thing: thing,
* otherthing: otherthing
* });
*/
function leakHunt(root) {
let path = [];
let seen = [];
try {
let output = leakHunt.inner(root, path, seen);
output.forEach(function (line) {
dump(line + "\n");
});
} catch (ex) {
dump(ex + "\n");
}
}
leakHunt.inner = function (root, path, seen) {
let prefix = new Array(path.length).join(" ");
let reply = [];
function log(msg) {
reply.push(msg);
}
let direct;
try {
direct = Object.keys(root);
} catch (ex) {
log(prefix + " Error enumerating: " + ex);
return reply;
}
try {
let index = 0;
for (let data of root) {
let prop = "" + index;
leakHunt.digProperty(prop, data, path, seen, direct, log);
index++;
}
} catch (ex) {
/* Ignore things that are not enumerable */
}
for (let prop in root) {
let data;
try {
data = root[prop];
} catch (ex) {
log(prefix + " " + prop + " = Error: " + ex.toString().substring(0, 30));
continue;
}
leakHunt.digProperty(prop, data, path, seen, direct, log);
}
return reply;
};
leakHunt.hide = [ /^string$/, /^number$/, /^boolean$/, /^null/, /^undefined/ ];
leakHunt.noRecurse = [
/^string$/, /^number$/, /^boolean$/, /^null/, /^undefined/,
/^Window$/, /^Document$/,
/^XULDocument$/, /^XULElement$/,
/^DOMWindow$/, /^HTMLDocument$/, /^HTML.*Element$/, /^ChromeWindow$/
];
leakHunt.digProperty = function (prop, data, path, seen, direct, log) {
let newPath = path.slice();
newPath.push(prop);
let prefix = new Array(newPath.length).join(" ");
let recurse = true;
let message = leakHunt.getType(data);
if (leakHunt.matchesAnyPattern(message, leakHunt.hide)) {
return;
}
if (message === "function" && direct.indexOf(prop) == -1) {
return;
}
if (message === "string") {
let 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);
let lines = leakHunt.inner(data, newPath, seen);
if (lines.length == 0) {
if (message !== "function") {
log(prefix + prop + " = " + message + " { }");
}
} else {
log(prefix + prop + " = " + message + " {");
lines.forEach(function (line) {
log(line);
});
log(prefix + "}");
}
} else {
log(prefix + prop + " = " + message);
}
};
leakHunt.matchesAnyPattern = function (str, patterns) {
let match = false;
patterns.forEach(function (pattern) {
if (str.match(pattern)) {
match = true;
}
});
return match;
};
leakHunt.getType = function (data) {
if (data === null) {
return "null";
}
if (data === undefined) {
return "undefined";
}
let type = typeof data;
if (type === "object" || type === "Object") {
type = leakHunt.getCtorName(data);
}
return type;
};
leakHunt.getCtorName = function (obj) {
try {
if (obj.constructor && obj.constructor.name) {
return obj.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(obj).slice(8, -1);
};
|