Crash with setContext to image.

Codea seem to crash with this code after a few seconds. Out of memory I guess. Shouldn’t old image objects be handled by the gc?

I often trying to render to a image buffer for graphics thats updated less frequently, but it seems to run out of memory if I do. Should I create the image objects used only in setup?

function setup()
end

function draw()
    background(40, 40, 50)
    strokeWidth(5)
    fill(217, 35, 35, 255)
    
    img = image(585,580)
    setContext(img)
    rect(0,0,585,580)
    setContext()
    sprite(img, WIDTH/2, HEIGHT/2)
end

You should move the creation of your image into the setup function as you’ll quickly allocate a large number of images before garbage collection will kick in.

Lua only does garbage collection occasionally, but we do tell it to do it when a memory warning is given to us from the OS. Sometimes this doesn’t happen fast enough though, and the OS will just kill the app in that case. There isn’t much we can do about it sadly, aside from what @John mentioned.

Ok, thanks. Then I’ll make sure to move image allocation to setup, or saving images in a table so that I can reuse them.