Fix: stop media streams in p5.prototype.remove() by calling element.r…#8547
Fix: stop media streams in p5.prototype.remove() by calling element.r…#8547Sanchit2662 wants to merge 1 commit intoprocessing:mainfrom
Conversation
…emove() Signed-off-by: Sanchit2662 <sanchit2662@gmail.com>
|
🎉 Thanks for opening this pull request! For guidance on contributing, check out our contributor guidelines and other resources for contributors! Thank You! |
|
Hi @davepagurek , please review the pr. |
|
Hi @davepagurek , is there any update on this PR, or would you like me to change anything? |
|
HI @Sanchit2662 please be sure to follow contributor guidelines in the future: issue first, then approval/assignment, then PR/work. Is there an issue alreayd open? Please link it |
Summary
p5.prototype.remove()insrc/core/main.jsmanually removes DOM nodes and event listeners fromthis._elements, but it does not call each element’s ownremove()method.Because of this, the media-specific cleanup inside
p5.Element.prototype.remove()(insrc/dom/dom.js) is skipped. When a sketch usescreateCapture(), callingmySketch.remove()does not stop the underlyinggetUserMediastream. The webcam/microphone remains active and hardware resources are not released.The issue is caused by duplicated cleanup logic in
main.jsthat does not include thep5.MediaElementmedia-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.