Skip to content

Commit 2339dfa

Browse files
committed
Fix memory leak
The window.onresize handlers registered for each editor would prevent them from ever getting collected.
1 parent 90aead7 commit 2339dfa

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

lib/codemirror.js

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1315,14 +1315,24 @@ window.CodeMirror = (function() {
13151315
on(d.scrollbarV, "mousedown", reFocus);
13161316
// Prevent wrapper from ever scrolling
13171317
on(d.wrapper, "scroll", function() { d.wrapper.scrollTop = d.wrapper.scrollLeft = 0; });
1318-
on(window, "resize", function resizeHandler() {
1319-
var currentParent = d.wrapper.parentNode;
1318+
1319+
if (!window.registered) window.registered = 0;
1320+
++window.registered;
1321+
function onResize() {
13201322
// Might be a text scaling operation, clear size caches.
13211323
d.cachedCharWidth = d.cachedTextHeight = null;
13221324
clearCaches(cm);
1323-
if (currentParent === document.body) updateDisplay(cm, true);
1324-
else off(window, "resize", resizeHandler);
1325-
});
1325+
updateDisplay(cm, true);
1326+
}
1327+
on(window, "resize", onResize);
1328+
// Above handler holds on to the editor and its data structures.
1329+
// Here we poll to unregister it when the editor is no longer in
1330+
// the document, so that it can be garbage-collected.
1331+
setTimeout(function unregister() {
1332+
for (var p = d.wrapper.parentNode; p && p != document.body; p = p.parentNode) {}
1333+
if (p) setTimeout(unregister, 5000);
1334+
else {--window.registered; off(window, "resize", onResize);}
1335+
}, 5000);
13261336

13271337
on(d.input, "keyup", operation(cm, function(e) {
13281338
if (cm.options.onKeyEvent && cm.options.onKeyEvent(cm, addStop(e))) return;

0 commit comments

Comments
 (0)