Audio: clean up event listeners and buffer view on destroy#75
Merged
Conversation
The Audio constructor binds three window-level event listeners (keydown / click / touchend) to drive startPlayback. They're only removed when startPlayback() fires; if the Emulator is destroyed before the user produces a gesture — or if an embedder recreates the Emulator multiple times before that happens — the listeners stayed attached to a stale Audio instance forever. Audio also holds a TypedArray view (this.buffer) into the wasm heap region returned by _get_audio_buffer_ptr. After Emulator destroy() calls _emulator_delete + _free, that backing memory is gone, and any held reference to the buffer view points at freed memory. Add Audio.destroy(): removes the gesture listeners and nulls out the buffer view. Call it from Emulator.destroy() before _emulator_delete so the cleanup happens while the wasm pointer is still valid.
There was a problem hiding this comment.
Pull request overview
This PR improves lifecycle cleanup in the browser demo’s audio subsystem to prevent stale window-level gesture listeners and to avoid retaining a TypedArray view into freed wasm memory after an Emulator instance is destroyed.
Changes:
- Call
this.audio.destroy()fromEmulator.destroy()before deleting the wasm emulator instance. - Add
Audio.destroy()to remove thekeydown/click/touchendgesture listeners (if still installed). - Clear
Audio’s wasm-backed buffer view on destroy to avoid referencing freed memory.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The Audio constructor binds three window-level event listeners (keydown / click / touchend) to drive startPlayback. They're only removed when startPlayback() fires; if the Emulator is destroyed before the user produces a gesture — or if an embedder recreates the Emulator multiple times before that happens — the listeners stayed attached to a stale Audio instance forever.
Audio also holds a TypedArray view (this.buffer) into the wasm heap region returned by _get_audio_buffer_ptr. After Emulator destroy() calls _emulator_delete + _free, that backing memory is gone, and any held reference to the buffer view points at freed memory.
Add Audio.destroy(): removes the gesture listeners and nulls out the buffer view. Call it from Emulator.destroy() before _emulator_delete so the cleanup happens while the wasm pointer is still valid.