Skip to content

Fix: stop media streams in p5.prototype.remove() by calling element.r…#8547

Open
Sanchit2662 wants to merge 1 commit intoprocessing:mainfrom
Sanchit2662:fix/mediaelement-remove-cleanup
Open

Fix: stop media streams in p5.prototype.remove() by calling element.r…#8547
Sanchit2662 wants to merge 1 commit intoprocessing:mainfrom
Sanchit2662:fix/mediaelement-remove-cleanup

Conversation

@Sanchit2662
Copy link
Contributor

@Sanchit2662 Sanchit2662 commented Feb 21, 2026

Summary

p5.prototype.remove() in src/core/main.js manually removes DOM nodes and event listeners from this._elements, but it does not call each element’s own remove() method.

Because of this, the media-specific cleanup inside p5.Element.prototype.remove() (in src/dom/dom.js) is skipped. When a sketch uses createCapture(), calling mySketch.remove() does not stop the underlying getUserMedia stream. The webcam/microphone remains active and hardware resources are not released.

The issue is caused by duplicated cleanup logic in main.js that does not include the p5.MediaElement media-stop branch.


Fix

The manual DOM + event listener cleanup loop in p5.prototype.remove() has been replaced with calls to each element’s own remove() method.

To avoid mutation issues (since element.remove() splices itself from _elements), iteration is performed over a shallow copy of the array.

By delegating cleanup to p5.Element.prototype.remove():

DOM nodes are removed as before

Event listeners are properly detached

_elements bookkeeping remains correct

Media elements now correctly stop active streams and release hardware devices

This change does not introduce new behavior — it ensures that existing element-level cleanup logic is consistently used during sketch teardown, preventing resource leaks while preserving existing functionality.

…emove()

Signed-off-by: Sanchit2662 <sanchit2662@gmail.com>
@welcome
Copy link

welcome bot commented Feb 21, 2026

🎉 Thanks for opening this pull request! For guidance on contributing, check out our contributor guidelines and other resources for contributors!
🤔 Please ensure that your PR links to an issue, which has been approved for work by a maintainer; otherwise, there might already be someone working on it, or still ongoing discussion about implementation. You are welcome to join the discussion in an Issue if you're not sure!
🌸 Once your PR is merged, be sure to add yourself to the list of contributors on the readme page !

Thank You!

@Sanchit2662
Copy link
Contributor Author

Hi @davepagurek , please review the pr.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

1 participant