I think I've narrowed it down enough to where I can be pretty certain this is where the bug is. After playing a Sample completely using a sequence, then replacing the Sample by using either track:setInstrument() or synth:setWaveform(), Playdate (in the simulator and on hardware) throws a "corrupted double-linked list` error and then freezes for up to 10 seconds, or it throws a "SoundSource (hex address) doesn't have a lua wrapper" error. Sometimes it does this, sometimes it does not.
Here's a Linux version of the test build I posted in that other bug just now: Dropbox - File Deleted - Simplify your life Mind giving it a test and see if it fixes the problem? If you want to test on the device, just send me your serial # and I'll put you in a test cohort.
So there must still be an edge case we're not handling properly. Can you send me a pdx that demonstrates this? Source shouldn't be necessary. I tried to put something together following your outline above but it didn't crash.
Aha, got it. It's a similar problem to the others but in a different place. Here the bug is that synth:copy() doesn't know anything about the specific type of synth so it does a plain copy of the underlying data. In this case we wind up with two references to the sample without bumping its retatin count; then when you replace the sample in the copy its retain count drops to zero and it gets freed even though there's still a reference to it. I've added a custom copy function in the implementation to fix this. In the mean time, you can work around it by not copying a synth with a sample set on it, either via synth.new(sample) or synth:setWaveform(sample).