Preference observers stop working after a few observations

RESOLVED INVALID

Status

()

RESOLVED INVALID
10 years ago
9 years ago

People

(Reporter: jason.barnabe, Unassigned)

Tracking

1.9.1 Branch
x86
Linux
Points:
---
Bug Flags:
blocking1.9.1 -

Firefox Tracking Flags

(Not tracked)

Details

(Reporter)

Description

10 years ago
Tested in Firefox 3.0.6 and 3.1b2 on Ubuntu.

The attached test extension adds a pref observer to browser windows that alerts whenever extensions.preftest.test changes.

1. Install the extension and close Firefox
2. Open one Firefox window
3. Go to about:config and toggle extensions.preftest.test a few times. You can see that it alerts the name of the pref and the value.
4. Open a second Firefox window
5. In the first window, toggle extensions.preftest.test some more. It should be alerting twice (once for each window).
6. Keep toggling

Expected results:

You keep getting two alerts every time you toggle the pref.

Actual Results: 

As you keep toggling, eventually one or both windows stop alerting. This usually happens around the 4th toggle with both windows open for me. This is blocking me from adding a new feature to my extension, so requesting blocking.

The relevant code in the extension is simply:

var prefTestOverlay = {

	init: function() {
		Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService).getBranch("extensions.preftest.").QueryInterface(Components.interfaces.nsIPrefBranch2).addObserver("", prefTestOverlay, false);
	},

	observe: function(subject, topic, data) {
		//Components.utils.reportError(data + " " + subject.QueryInterface(Components.interfaces.nsIPrefBranch2).getBoolPref(data));
		alert(data + " " + subject.QueryInterface(Components.interfaces.nsIPrefBranch2).getBoolPref(data));
	}

}

addEventListener("load", prefTestOverlay.init, false);
Flags: blocking1.9.1?
Not gonna block this release since it's not a regression.
Flags: blocking1.9.2?
Flags: blocking1.9.1?
Flags: blocking1.9.1-
The preference branch object that you added your observer to is being GCed, since you're not keeping a reference to it. You need to keep a reference to the preference branch to keep it alive. This should perhaps be better documented on the interface...
Status: NEW → RESOLVED
Last Resolved: 10 years ago
Resolution: --- → INVALID
Alternatively, you could add your observer on the root branch, which is kept alive by the preferences service (you can QI the preferences service to nsIPrefBranch[2], it forwards those calls to the root branch).

Updated

9 years ago
Flags: blocking1.9.2?
You need to log in before you can comment on or make changes to this bug.