0

I am using jQuery tab library in my Firefox extension. Tabs feature is working fine in my extension. However it is creating a zombie compartment.

This is how I am using tabs:

$j(mydiv).find('#targetid').tabs({selected: 2});

When I comment this line and install extension there is no memory leak. Any idea what is going on?

Wladimir Palant
  • 56,865
  • 12
  • 98
  • 126
MKumar
  • 1,524
  • 1
  • 14
  • 22

1 Answers1

1

The problem is that jQuery UI lives in the browser window and will stay "alive" as long as the browser window stays open. The tabs however are in a content tab, they will disappear as soon as you close that tab. jQuery UI must be keeping a local reference to the tabs somewhere and that reference doesn't let the tabs to get garbage collected even after the tab is closed (zombie compartment). One solution would be finding that reference and getting rid of it. But even if you solved it this way (I cannot since I don't know enough about jQuery) - the issue will likely come back later because jQuery UI is designed to run in a web page, this kind of memory leaks isn't considered there.

A more robust solution would be running jQuery UI in the same context as the UI it is responsible for. This way when you close the tab both jQuery UI and its widgets will be disposed - no more zombie compartments. This could be done using the message manager for example:

// Load content script into the current tab
var contentScriptURL = "chrome://.../content/contentScript.js";
gBrowser.selectedBrowser.messageManager.loadFrameScript(contentScriptURL, false);

And contentScript.js would then load jQuery and jQuery UI into its context using mozIJSSubscriptLoader:

var scriptLoader = Components.classes["@mozilla.org/moz/jssubscript-loader;1"]
                     .getService(Components.interfaces.mozIJSSubScriptLoader);
scriptLoader.loadSubScript("chrome://.../content/jquery.js");
var jQuery = jQuery.noConflict(true);
scriptLoader.loadSubScript("chrome://.../content/jquery-ui.js", jQuery);
...
$j(mydiv).find('#targetid').tabs({selected: 2});

This content script has the same chrome privileges as the scripts running in your overlay but it will be unloaded once the tab is closed - and along with it any references it might be keeping to contents of that tab.

Wladimir Palant
  • 56,865
  • 12
  • 98
  • 126
  • Thanks Wladimir! Will try this and get back to you. – MKumar May 25 '12 at 08:52
  • Below is my content script ===============================(function(){ var scriptLoader = Components.classes["@mozilla.org/moz/jssubscript-loader;1"] .getService(Components.interfaces.mozIJSSubScriptLoader); scriptLoader.loadSubScript("chrome://aqqin/content/js/jquery-1.7.1.js", content); var jQ = content.jQuery.noConflict(); scriptLoader.loadSubScript("chrome://aqqin/content/js/jquery.ui.tabs.min.js", jQ); })(); ============================================================== Getting error content.jQuery is undefined – MKumar May 25 '12 at 11:38
  • A content script has nothing to conflict with - nothing else is running in this context (web page scripts cannot "see" you). So why load scripts into a variable? – Wladimir Palant May 25 '12 at 12:27
  • I tried with below scriptLoader.loadSubScript("chrome://aqqin/content/js/jquery-1.7.1.js"); scriptLoader.loadSubScript("chrome://aqqin/content/js/jquery.ui.tabs.min.js"); =================================== Gives error "window is undefined" while loading jquery. I also tried with scriptLoader.loadSubScript("chrome://aqqin/content/js/jquery-1.7.1.js",content); scriptLoader.loadSubScript("chrome://aqqin/content/js/jquery.ui.tabs.min.js"); ============== Gives error "jQuery is undefined" while loading jquery.ui.tabs.min.js – MKumar May 25 '12 at 13:10
  • I guess loading jquery.ui.tabs.min.js need a jquery context – MKumar May 25 '12 at 13:11
  • @DineshAtoliya: Ok, I've updated the code to load jQuery - this way it should work. – Wladimir Palant May 25 '12 at 13:14
  • scriptLoader.loadSubScript("chrome://.../content/jquery.js"); this line itself gives error "window is not defined" . I also look the jquery code as where it uses window. on last page of jquery libs below code is written . window.jQuery = window.$ = jQuery; – MKumar May 29 '12 at 11:48
  • Finally I got succeeded with loading jquery. This also help me http://stackoverflow.com/questions/8376416/how-to-use-jquery-in-content-script-in-a-firefox-mobile-fennec-extension. However I running into other issue, my content script only stick in browser if I keep open the "about:memory"/"about:addons"/"about:compartments" tab. Other wise code written in content script doesn't run – MKumar May 31 '12 at 10:35