Codea Craft crashes

@Simeon I noticed this as I played with the Voxel Terrain example. I took some code from it and cut it down to this code just so it runs faster. Plus maybe it will be easier to find the cause. If I run this code, exit back to the editor, and run it again, Codea will crash on the 5th run. Occasionally it will crash on the 4th run, but mostly on the 5th and never past the 5th run. This is on my 16GB iPad Air. It doesn’t crash on my 128GB iPad Pro so it’s probably something to do with memory. Apparently something isn’t being cleared when Codea is restarted.

function setup()  
    scene = craft.scene()
    scene.voxels:resize(vec3(100,1,100))
    viewer = scene.camera:add(OrbitViewer, vec3(0,0,0), 200, 50, 400)
    createCloud(-25,-25,-25)
end

function update(dt)
    scene:update(dt)
end

function draw()
    update(DeltaTime)
    scene:draw()
end

function createCloud(x,y,z)
    local c = scene:entity()
    c.position = vec3(x,y,z)
    local v = c:add(craft.volume, 50, 50, 50)
    local cx, cy, cz = v:size() 
    local white = color(255, 255, 255, 255)    
    local f = .1
    local power = 4    
    local wx = craft.noise.perlin()
    wx.seed = x
    wx.frequency = f
    local wy = craft.noise.perlin()
    wy.seed = y    
    wy.frequency = f 
    local wz = craft.noise.perlin()
    wz.seed = z
    wz.frequency = f 
    for i = 0,cx-1 do
        for j = 0,cy-1 do
            for k = 0,cz-1 do
                local ox, oy, oz = wx:getValue(i,j,k), 
                    wy:getValue(i,j,k), wz:getValue(i,j,k)
                local dx, dy, dz = cx/2 - (i + ox * power), 
                    cy/2 - (j + oy * power), cz/2 - (k + oz * power)
                local d = dx*dx + dy*dy + dz*dz
                if d < 100 then
                    v:set(i,j,k, BLOCK_ID, 1, COLOR, white) 
                end
            end
        end        
    end    
end

@dave1707 put this in the draw() function and shows memory climbing


output.clear()
print("KB Memory used  "..collectgarbage("count"))

@dave1707 try putting in the function update().


garbagecollect()

Does look like the Voxel library has a leak!

@Bri_G Watching memory was the first thing that I tried. I could see the memory go thru the rise and fall as Codea does the normal collectgarbage cycle. If the program is left running, it runs fine so it’s not the normal memory usage. I think that Craft uses an internal table for something and when Codea is first started, everything is cleared. But when the program is constantly stopped and re-started, that internal table isn’t cleared and on the 5th restart it exceeds the size allocated for it and crashes Codea. @Simeon or @John will have to investigate that.