K-Meleon: Use a different approach to save timers (prevents a memory leak) ADBLOCK_PLUS_1_0_BRANCH
authorWladimir Palant <trev@adblockplus.org>
Wed, 23 Jun 2010 13:37:32 +0200
branchADBLOCK_PLUS_1_0_BRANCH
changeset 2124 f31040367dc5
parent 2123 64851199d6df
child 2125 ef444f7712ec
K-Meleon: Use a different approach to save timers (prevents a memory leak)
kmeleon/window.js
--- a/kmeleon/window.js
+++ b/kmeleon/window.js
@@ -13,34 +13,35 @@ function removeEventListener(event, hand
   if (event == "load")
     _loadListeners = _loadListeners.filter(function(item) {return item != handler});
 }
 function _notifyLoadListeners() {
   for (var i = 0; i < _loadListeners.length; i++)
     _loadListeners[i].call(window);
 }
 
-var _timers = [];
+var _timers = {last: 0};
 function setInterval(callback, delay) {
   var timer = Components.classes["@mozilla.org/timer;1"]
                         .createInstance(Components.interfaces.nsITimer);
   timer.init({observe: callback}, delay, Components.interfaces.nsITimer.TYPE_REPEATING_SLACK);
-  _timers.push(timer);
+  _timers[++_timers.last] = timer;
 }
 function setTimeout(callback, delay) {
+  var index = ++_timers.last;
   var timer = Components.classes["@mozilla.org/timer;1"]
                         .createInstance(Components.interfaces.nsITimer);
   timer.init({
-    observe: function(){
-      delete _timers[this.index];
+    observe: function() {
       callback();
-    },
-    index: _timers.length
+      delete _timers[index];
+      timer = null;
+    }
   }, delay, Components.interfaces.nsITimer.TYPE_ONE_SHOT);
-  _timers.push(timer);
+  _timers[index] = timer;
 }
 
 function delayedOpenTab(url)
 {
   var hWnd = (_currentWindow && !_currentWindow.closed ? _windowMediator.toHWND(_currentWindow) : null);
   openTab(url, hWnd);
 }
 
@@ -67,9 +68,9 @@ var _classInfo = {
     count.value = 0;
     return [];
   },
   QueryInterface: window.QueryInterface
 }
 
 this.__defineGetter__("content", function() {
   return (_currentWindow && !_currentWindow.closed ? _currentWindow : {location: {href: "about:blank"}});
-});
\ No newline at end of file
+});