Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Codea crash because of this code

edited April 2019 in Questions Posts: 21

There is something wrong with this code. Codea constantly crashes after this method is called for some 500 times from the draw().

function Screen:scroll(pos, dim, lines)
    fill(0, 0, 0, 255)

    local topLeft = self:calculateActualLoc(pos)
    local bottomRight = vec2(topLeft.x+dim.x*8, topLeft.y-dim.y*8)
    if lines < 0 then
        local movedArea = self.buffer:copy(topLeft.x-1, bottomRight.y, dim.x*8, (dim.y+lines)*8+1)
        sprite(movedArea, topLeft.x-1, topLeft.y-movedArea.height)
        rect(topLeft.x-1, bottomRight.y, dim.x*8, -lines*8)
    elseif lines > 0 then
        local movedArea = self.buffer:copy(topLeft.x-1, bottomRight.y+lines*8, dim.x*8, (dim.y-lines)*8+1)
        sprite(movedArea, topLeft.x-1, bottomRight.y-1)
        rect(topLeft.x-1, topLeft.y-lines*8, dim.x*8, lines*8)


I narrowed it further to the call to image:copy() but I can’t reproduce it in a separate plain simple program. If the copy line is commented out, no crash but of course it doesn’t do what it’s supposed to.

self.buffer is just image(pixelDim.x, pixelDim.y) whose contents are drawn via both image:set and graphic operations via setContext(self.buffer) just like what this method does, all that before it’s sprited onto screen.

The symptoms look to me that there’s some critical resource leak here when image:copy is called, causing Codea to crash. But I’m not sure why same problem doesn’t show up when I simplified it into a plain separate program. I need help to get around this problem.



  • dave1707dave1707 Mod
    Posts: 9,979

    Try putting collectgarbage() at the start of Screen:scroll. There's probably a problem with memory not being cleared fast enough by itself.

  • Posts: 21

    @dave1707 THANK YOU! It works beautifully. Do you know under what conditions that memory doesn’t get cleared fast enough? When does it get cleared anyway?

  • SimeonSimeon Admin Mod
    Posts: 5,778

    @tsukit I think your code exposes a bug with how Lua interacts with C here. Lua doesn't think it's using a lot of memory since it's just holding onto light weight pointers to the image data, in C the memory keeps being allocated by the image library, but Lua doesn't know that it should probably collect garbage because those light pointers refer to large amounts of memory

    We need to somehow track the memory usage outside of Lua and trigger a Lua GC if it starts getting out of hand

  • Posts: 21

    @Simeon that makes sense. Thanks for further info.

Sign In or Register to comment.