Repository navigation
Description
Description:
Removing an <a-scene> from the DOM leaves nine listeners behind on window, document and navigator.xr. Mount and unmount a scene N times and you end up with N copies of each. Most of the handlers close over the scene element, so the detached scene and its components stay reachable. In a single-page app that mounts a scene per route, this grows with every navigation.
Listeners added and never removed, with line numbers at v1.8.0 (master is the same):
src/core/scene/a-scene.js:166,attachedCallbackPostCamera: an anonymouswindowresizehandler that callsself.resize().disconnectedCallbackremovessessionend, but not this one.src/core/scene/a-scene.js:157: an anonymousnavigator.xrsessiongrantedhandler that callsself.enterVR().src/core/scene/a-scene.js:927-930,setupCanvas:fullscreenchange,mozfullscreenchange,webkitfullscreenchangeandMSFullscreenChangeondocument, withonFullScreenChange. That function is declared insidesetupCanvasand closes oversceneEl, so nothing outside can remove it.src/components/scene/screenshot.js:108:play()adds awindowkeydownlistener. The component has nopause()orremove().src/components/scene/xr-mode-ui.js:48and:56:init()adds an anonymouswindowmessagehandler and awindoworientationchangehandler.remove()(line 127) leaves both.
The same lines are in 1.5.0, 1.6.0, 1.7.0 and 1.7.1. #3463 and #4899 describe memory that a removed scene does not release; this issue lists the listeners behind part of it.
To reproduce:
- Open a page that creates
<a-scene>from script and removes it again, a few times over, for exampledocument.body.appendChild(scene), wait forloaded, thenscene.remove(). - In the Chrome DevTools console, run
getEventListeners(window),getEventListeners(document)andgetEventListeners(navigator.xr)after each removal. resize,keydown,message,orientationchange,sessiongrantedand each of the four fullscreen events gain one entry per mount.
We measured this in a React app (aframe 1.5.0, Chrome) over three mount and unmount cycles: each listener above went 1, 2, 3.
Possible fix:
Keep a reference to each handler and remove it on teardown:
-
In
a-scene.js, store the resize handler, thesessiongrantedhandler andonFullScreenChangeon the scene (for examplethis.onWindowResize, set insetupCanvasassceneEl.onFullScreenChange). Then remove them indisconnectedCallbacknext to the existingsessionendremoval. -
In
screenshot.js, addpause()(orremove()) that removesthis.onKeyDown. -
In
xr-mode-ui.js, store themessagehandler as a bound method and remove it andtoggleOrientationModalIfNeededwhen the scene is detached.update()also callsremove()whenenabledis false, so the removal must not run there while the scene stays attached. -
A-Frame Version: 1.5.0 (lines checked at 1.8.0 and master)
-
Platform / Device: Chrome, desktop
Activity
vincentfretin commentedon Nov 29, 2026
All this are indeed valid, can your agent do a PR that fixes this, with a unit test that assert the listeners are all removed? For screenshot, implement the
removeEventListenerinpause()only.Be aware of the pending #5868 it's funny it didn't report it, probably because
window.addEventListener('load', undefined);doesn't register anything.