Crash (segmentation fault in simulator) in lua game when exiting playdate.update()

I am working on game using Lua, and started experiencing random crashes on hardware and segmentation faults with the simulator on MacOS.

Finally, i found a way to reproduce the crash consistently, and added some print statements to try to find the cause.

The code is this:

print("Main loop...")
function playdate.update()
    print("a")
    if gameStatus == 0 then
        print("b")
        UpdateMenu()
        print("c")
    elseif gameStatus == 1 then
        print("d")
        gameUpdate()
        print("e")
        gfx.sprite.update()
        print("f")
    end
    print("g")
end
print("end of line")

And the log on the shell (using the simulator console is not possible because of the segmentation fault) is like this

Main Loop...
a
b
c
g
a
b
c
g
a
d
e
f
(...)
g
a
d
e
f
g
a
d
e
f
g
[1]    10028 segmentation fault   game.pdx

Tried with SDK 1.12.3, 1.13.0, etc. up to 1.13.5 always with the same result.

Is there any way that i can get more information to find the source of the problem?

Thank you for your help.

Also, here is a crashlog when using the real device

--- crash at 2023/04/28 19:03:02---
build:e28b7d0d163a-1.13.5-release.152889-buildbot
   r0:00000001    r1:00000001     r2:a0000000    r3: 40012c00
  r12:00003fe0    lr:0803145f     pc:08031418   psr: 81000000
 cfsr:00000082  hfsr:00000000  mmfar:0008000c  bfar: 0008000c
rcccsr:00000000
heap allocated: 582624
Lua totalbytes=127819309 GCdebt=-127575320 GCestimate=232185 stacksize=21

Can you add a full game that reproduces the issue to this report?

Yes, of course. Tried also with 11.3.6.

After bisecting and removing chunks of the source, if have isolated the problem to a single line of code:

image

conveyTable = gfx.imagetable.new(4, 1, 8 * lscale)

lscale is a variable that can be either 1 or 1.5 (so 8*lscale is either 8 or 12)

Here is also a small video showing the problem happen

Crash.mov.zip (1.5 MB)

Oops!

Re-reading the documentation, now i see, that the third argument to gfx.imagetable.new is not (as i somehow supposed) the width in pixels for the images, but some kind of memory allocation size :scream:.

Removing that third argument fixes all the crashes.

Sorry for wasting your time :disappointed_relieved:

No problem, there still appears to be a bug here someplace, it shouldn't be crashing. We're investigating.