Sequence.new crashes with stack overflow on repeated invocation

When repeatedly loading midi file, a stack overflow occurs:

/Games/midi_master.pdx
stack overflow at Core/minilua/lapi.c:510
stack traceback:
	[C]: in ?

This code survives for about 10 frames on simulator; fewer on device:

local playdate <const> = playdate

function playdate.update()
    print("creating sequence")
    playdate.sound.sequence.new("songs/Rollinginthedeep.mid")
end

Minimal sample (code + pdx)

sequence_stack_overflow.zip (34.4 KB)

Looks like it's this bug: How to free up memory from a sound.track? Though I'm not sure why it manifests as a stack overflow. It's an out of memory problem, anyway, and that can have weird side effects when not handled properly.

Yeah, its sure seems to be, when I disable the memory limit in the simulator, the script keeps running and the simulator's memory usage keeps growing fast.

The mentioned workaround, If I understood correctly, doesn't work however:

local playdate <const> = playdate

function playdate.update()
    print("creating sequence")
    local sequence = playdate.sound.sequence.new("songs/Rollinginthedeep.mid")
    for i = 1, sequence:getTrackCount() do
        sequence:getTrackAtIndex(i):clearNotes()
    end
    print("GC", collectgarbage("count")) 
end

prints 91.4024 on every iteration, but simulator still crashes when hitting the memory limit

That workaround isn't perfect--clearing notes frees the event list memory but the track itself is still leaking, possibly including controller messages which could take up a fair bit of memory. Hopefully we can get that fix out soon. Until then, caching the loaded sequences is probably the best workaround.