Skip to content

Audio: clean up event listeners and buffer view on destroy#75

Merged
binji merged 1 commit into
binji:mainfrom
kwigbo-org:pr/audio-destroy
May 13, 2026
Merged

Audio: clean up event listeners and buffer view on destroy#75
binji merged 1 commit into
binji:mainfrom
kwigbo-org:pr/audio-destroy

Conversation

@kwigbo
Copy link
Copy Markdown
Contributor

@kwigbo kwigbo commented May 11, 2026

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.

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.
Copilot AI review requested due to automatic review settings May 11, 2026 19:06
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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() from Emulator.destroy() before deleting the wasm emulator instance.
  • Add Audio.destroy() to remove the keydown/click/touchend gesture 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.

@binji binji merged commit 9ff6e8a into binji:main May 13, 2026
9 of 10 checks passed
@kwigbo kwigbo deleted the pr/audio-destroy branch May 14, 2026 12:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants