When I play any sound thousands of times in a row,
DOM Nodes count will become very high and they are never deleted.
When loading Snap!:
When i run the script
it results in this:
Eventually, corrupted audio will be heard. and too many dom nodes can crash Snap!
How can I fix this?
Because it's a serious issue and should be fixed
these nodes should eventually get garbage collected when nothing holds on to them anymore
Please send some sort of proof that this happens.
(Also, even if the corrupted audio eventually fixes itself, the fact that it happens in the first place is still a problem.)
huh? What are you talking about? There's no "corrupted" audio, If you play thousands of sounds at the same time what do you expect? Please don't bump stuff like this.
In one of my projects, the longer you play it for, the laggier the game is and the more choppy and weird the sounds are. That's because of the DOM nodes generating and not disappearing. @cedricpinball's example wasn't necessarily about running all those blocks at once, the same thing happens if you spread them out.
The reason for high Dom node count is this:
Detached HTMLAudioElement and Detached MediaElementAudioSourceNode.
These two DOM nodes are never garbage collected. High Dom nodes count require more memory which can slow down or even crash Snap! with "out of memory" error.
Pressing the "Collect garbage" button at memory in dev tools does nothing to these Dom nodes.
Just delete the DOM Nodes when its done playing the sound, it will only take a few lines of JS.
The sound in Snap! has always been kind of weird.
Bump. This huge problem that fundamentally breaks audio in Snap! still hasn't been fixed.
never occurs to me, I'm making sounds and music like crazy without any problems whatsoever
Apparently the garbage collector which is in Google's V8 engine which is what Chrome and Edge use does not garbage collect any dom nodes that are created using createMediaElementSource.
But Firefox use SpiderMonkey which does collect the dom nodes which means it's a browser bug (more specifically a bug in V8's garbage collector).
What makes it more strange is that if you use the "play sound "sound name" at # Hz" block, the bug is not there.
This is because the "play sound "sound name" at # Hz" block uses createBufferSource which does not have the issue that the normal play sound block (which uses createMediaElementSource) have.
"Detached nodes" are elements removed from the DOM but still referenced by some JS objects. Nothing wrong with GC.
Just to be sure - you are trying to play 2000 sounds at once in a tight loop (warp)?
It's quite obvious that the browser should have a common-sense limit of the count of the concurrently played audio streams. Other sounds should be queued, paused, or discarded to prevent resource exhaustion or DOS attacks.
@jens Then, the solution seems obvious: change the "play sound" block to use the same function as the "play sound at # Hz" block...