Title: [134627] trunk/Source/WebCore
Revision
134627
Author
pfeld...@chromium.org
Date
2012-11-14 10:48:59 -0800 (Wed, 14 Nov 2012)

Log Message

Web Inspector: keep track of mutation observers and disconnect them upon upload
https://bugs.webkit.org/show_bug.cgi?id=102239

Reviewed by Vsevolod Vlasov.

Otherwise we hit memory leaks.

* inspector/front-end/DefaultTextEditor.js:
(WebInspector.DefaultTextEditor.prototype.wasShown):
(WebInspector.DefaultTextEditor.prototype.willHide):
(WebInspector.TextEditorMainPanel.prototype._wasShown):
(WebInspector.TextEditorMainPanel.prototype._willHide):
(WebInspector.TextEditorMainPanel.prototype._attachMutationObserver):
(WebInspector.TextEditorMainPanel.prototype._detachMutationObserver):
* inspector/front-end/utilities.js:

Modified Paths

Diff

Modified: trunk/Source/WebCore/ChangeLog (134626 => 134627)


--- trunk/Source/WebCore/ChangeLog	2012-11-14 18:43:21 UTC (rev 134626)
+++ trunk/Source/WebCore/ChangeLog	2012-11-14 18:48:59 UTC (rev 134627)
@@ -1,3 +1,21 @@
+2012-11-14  Pavel Feldman  <pfeld...@chromium.org>
+
+        Web Inspector: keep track of mutation observers and disconnect them upon upload
+        https://bugs.webkit.org/show_bug.cgi?id=102239
+
+        Reviewed by Vsevolod Vlasov.
+
+        Otherwise we hit memory leaks.
+
+        * inspector/front-end/DefaultTextEditor.js:
+        (WebInspector.DefaultTextEditor.prototype.wasShown):
+        (WebInspector.DefaultTextEditor.prototype.willHide):
+        (WebInspector.TextEditorMainPanel.prototype._wasShown):
+        (WebInspector.TextEditorMainPanel.prototype._willHide):
+        (WebInspector.TextEditorMainPanel.prototype._attachMutationObserver):
+        (WebInspector.TextEditorMainPanel.prototype._detachMutationObserver):
+        * inspector/front-end/utilities.js:
+
 2012-11-14  Sergio Villar Senin  <svil...@igalia.com>
 
         [Qt] Use a node image if there is no drag image set for Drag&Drop

Modified: trunk/Source/WebCore/inspector/front-end/DefaultTextEditor.js (134626 => 134627)


--- trunk/Source/WebCore/inspector/front-end/DefaultTextEditor.js	2012-11-14 18:43:21 UTC (rev 134626)
+++ trunk/Source/WebCore/inspector/front-end/DefaultTextEditor.js	2012-11-14 18:48:59 UTC (rev 134627)
@@ -560,7 +560,7 @@
 
         this._boundSelectionChangeListener = this._mainPanel._handleSelectionChange.bind(this._mainPanel);
         document.addEventListener("selectionchange", this._boundSelectionChangeListener, false);
-        this._mainPanel._attachMutationObserver();
+        this._mainPanel._wasShown();
     },
 
     _handleFocused: function()
@@ -571,7 +571,7 @@
 
     willHide: function()
     {
-        this._mainPanel._detachMutationObserver();
+        this._mainPanel._willHide();
         document.removeEventListener("selectionchange", this._boundSelectionChangeListener, false);
         delete this._boundSelectionChangeListener;
 
@@ -1239,16 +1239,34 @@
 }
 
 WebInspector.TextEditorMainPanel.prototype = {
+    _wasShown: function()
+    {
+        this._isShowing = true;
+        this._attachMutationObserver();
+    },
+
+    _willHide: function()
+    {
+        this._detachMutationObserver();
+        this._isShowing = false;
+    },
+
     _attachMutationObserver: function()
     {
+        if (!this._isShowing)
+            return;
+
         if (this._mutationObserver)
             this._mutationObserver.disconnect();
-        this._mutationObserver = new WebKitMutationObserver(this._handleMutations.bind(this));
+        this._mutationObserver = new NonLeakingMutationObserver(this._handleMutations.bind(this));
         this._mutationObserver.observe(this._container, { subtree: true, childList: true, characterData: true });
     },
 
     _detachMutationObserver: function()
     {
+        if (!this._isShowing)
+            return;
+
         if (this._mutationObserver) {
             this._mutationObserver.disconnect();
             delete this._mutationObserver;

Modified: trunk/Source/WebCore/inspector/front-end/utilities.js (134626 => 134627)


--- trunk/Source/WebCore/inspector/front-end/utilities.js	2012-11-14 18:43:21 UTC (rev 134626)
+++ trunk/Source/WebCore/inspector/front-end/utilities.js	2012-11-14 18:48:59 UTC (rev 134627)
@@ -866,3 +866,46 @@
     xhr.send(null);
     window.eval(xhr.responseText + "\n//@ sourceURL=" + scriptName);
 }
+
+
+/**
+ * Mutation observers leak memory. Keep track of them and disconnect
+ * on unload.
+ * @constructor
+ * @param {function(Array.<WebKitMutation>)} handler
+ */
+function NonLeakingMutationObserver(handler)
+{
+    this._observer = new WebKitMutationObserver(handler);
+    NonLeakingMutationObserver._instances.push(this);
+}
+
+NonLeakingMutationObserver._instances = [];
+
+NonLeakingMutationObserver.prototype = {
+    /**
+     * @param {Element} element
+     * @param {Object} config
+     */
+    observe: function(element, config)
+    {
+        if (this._observer)
+            this._observer.observe(element, config);
+    },
+
+    disconnect: function()
+    {
+        if (this._observer)
+            this._observer.disconnect();
+        NonLeakingMutationObserver._instances.remove(this);
+        delete this._observer;
+    }
+}
+
+if (!window.testRunner) {
+    window.addEventListener("unload", function() {
+        while (NonLeakingMutationObserver._instances.length)
+            NonLeakingMutationObserver._instances[NonLeakingMutationObserver._instances.length - 1].disconnect();
+    }, false);
+}
+
_______________________________________________
webkit-changes mailing list
webkit-changes@lists.webkit.org
http://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to