Skip to content

Commit a842dcd

Browse files
committed
do not retain ref to the first editor (fixes ajaxorg#2469)
1 parent 0dcb128 commit a842dcd

File tree

2 files changed

+64
-4
lines changed

2 files changed

+64
-4
lines changed

experiments/debug_mem_leak.html

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8">
5+
<script src="../demo/kitchen-sink/require.js"></script>
6+
<script type="text/javascript">
7+
require.config({
8+
paths: { ace: "../lib/ace" },
9+
waitSeconds: 0
10+
});
11+
</script>
12+
</head>
13+
<body>
14+
<p><button onclick="toggleEditor()">Toggle editor</button></p>
15+
16+
<div id="container"></div>
17+
<script>
18+
var editor;
19+
var counter = 0
20+
21+
function toggleEditor() {
22+
if (!editor) {
23+
var root = document.createElement("div");
24+
root.style.height = "100px";
25+
root.setAttribute("id", "editor");
26+
root.textContent = "function foo(items) {\nvar x = 'All this is syntax highlighted';\nreturn x;\n}";
27+
28+
document.getElementById("container").appendChild(root);
29+
30+
editor = ace.edit(root);
31+
32+
if (counter++ % 2)
33+
editor.setTheme("ace/theme/monokai");
34+
else
35+
editor.setTheme("ace/theme/clouds");
36+
37+
editor.session.setMode("ace/mode/javascript");
38+
} else {
39+
editor.destroy();
40+
var el = editor.container;
41+
el.parentNode.removeChild(el);
42+
43+
editor.container = null
44+
editor.renderer = null
45+
46+
editor = null;
47+
48+
var root = document.getElementById("editor")
49+
if (root)
50+
root.parentNode.removeChild(root);
51+
}
52+
}
53+
require(["ace/ace"], function(ace) {
54+
window.ace = ace;
55+
toggleEditor();
56+
})
57+
</script>
58+
</body>
59+
</html>

lib/ace/lib/event.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -326,13 +326,14 @@ exports.addCommandKeyListener = function(el, callback) {
326326
});
327327

328328
if (!pressedKeys) {
329-
pressedKeys = Object.create(null);
330-
addListener(window, "focus", function(e) {
331-
pressedKeys = Object.create(null);
332-
});
329+
resetPressedKeys();
330+
addListener(window, "focus", resetPressedKeys);
333331
}
334332
}
335333
};
334+
function resetPressedKeys(e) {
335+
pressedKeys = Object.create(null);
336+
}
336337

337338
if (window.postMessage && !useragent.isOldIE) {
338339
var postMessageId = 1;

0 commit comments

Comments
 (0)