Closed Bug 716751 Opened 14 years ago Closed 14 years ago

matchMedia() listeners lost

Categories

(Core :: DOM: CSS Object Model, defect, P2)

defect

Tracking

()

RESOLVED FIXED
mozilla13

People

(Reporter: webkit, Assigned: dbaron)

Details

Attachments

(1 file)

User Agent: Mozilla/5.0 (Windows NT 5.1) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.26 Safari/535.11 Steps to reproduce: window.matchMedia("screen and (max-width:600px)").addListener(function(media){ console.log("HERE: " + window.innerWidth); }); Then resized my browser several times. Actual results: The event handler fired three times and then stopped. In some cases, it fired zero times. Expected results: It should have continued to fire repeatedly. The only way to make this happen was to cache the MediaQueryList object, such as: var mql = window.matchMedia("screen and (max-width:600px)"); mql.addListener(function(media){ console.log("HERE: " + window.innerWidth); });
Yeah, we need to hold an ownin reference from the window/document/presshell to the returned object as long as it has listeners registered. Also, this should have used events, oh well.
Assignee: nobody → dbaron
Component: Untriaged → Layout
Product: Firefox → Core
QA Contact: untriaged → layout
Version: 9 Branch → Trunk
Assignee: dbaron → nobody
Component: Layout → DOM: CSS Object Model
QA Contact: layout → style-system
Assignee: nobody → dbaron
OS: Windows XP → All
Hardware: x86 → All
Status: UNCONFIRMED → ASSIGNED
Ever confirmed: true
Attached patch patchSplinter Review
Attachment #601686 - Flags: review?(bzbarsky)
Comment on attachment 601686 [details] [diff] [review] patch mListeners is a fallible array, right? So we could addref but fail to append in AddListener, then end up leaking on the next AddListener call, as far as I can tell.
Attachment #601686 - Flags: review?(bzbarsky) → review-
Oh, for some reason I read that as InfallibleTArray. I'd sort of thought that's what I'd written, too. But so it is. The most straightforward fix would seem to be (in AddListener): if (!mListeners.Contains(aListener)) { mListeners.AppendElement(aListener); + if (!HasListeners()) { + // Append failed; undo the AddRef above. + NS_RELEASE_THIS(); + } }
Yes. r=me with that change.
Status: ASSIGNED → RESOLVED
Closed: 14 years ago
Resolution: --- → FIXED
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: