Howdy, Stranger!

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

App is always crashing

edited October 2017 in Bugs Posts: 18

I just want to report a bug in hope of a fix in the next update.
Codea will always crash after a few seconds with the following code.
Specs:
Codea Version 2.5 (90)
iOS: 11.0.1 also in 11.1 beta 2
Device: iPad Pro ML0NF2D/A (the first iPad Pro)

Create a new project an paste the folowing code into it.
In my case. I splitted the classes into separated files.
Code:

--# Main
displayMode(FULLSCREEN)

function setup()

    physics.gravity(0, -1000)

    boxes = {}
    grounds = {}

    time = 0

    table.insert(grounds, Ground(-WIDTH / 2, 50, WIDTH / 2))
    table.insert(grounds, Ground(WIDTH / 2, 50, WIDTH / 2))

    textMode(CORNER)
end


function draw()

    background(40, 40, 50)

    if ElapsedTime - time > 0.2 then
        table.insert(boxes, Box(200 + math.random(200), HEIGHT + 200, 50))
        time = ElapsedTime
    end

    for i, box in pairs(boxes) do
        if box.body.y < 0 then
            box = nil
            table.remove(boxes, i)
        end
    end

    for i, box in pairs(boxes) do        
        box:draw()
    end

    for i, ground in pairs(grounds) do
        ground.body.linearVelocity = vec2(100, 0)
        if ground.body.x > WIDTH + WIDTH/2 then
            ground.body.x = - WIDTH / 2
        end
        ground:draw()
    end

    text(#boxes, 50, HEIGHT - 20)
    text(ElapsedTime, 50, HEIGHT - 40)
end

function touched(touch)
    if touch.state == BEGAN then
        table.insert(boxes, Box(touch.x, touch.y, 50))
    end
end

--# Box
Box = class()

function Box:init(x, y, size)
    self.body = physics.body(POLYGON, vec2(-size, -size), vec2(size, -size), vec2(size, size), vec2(-size, size))
    self.body.interpolate = true
    self.body.x = x
    self.body.y = y
    self.body.angle = math.random() * 360

    self.body.friction = 0.5

    self.mesh = mesh()
    self.mesh.vertices = triangulate(self.body.points)
    self.mesh:setColors(255,0,0, 255)

    self.sprites = {
        "Cargo Bot:Title Large Crate 1",
        "Cargo Bot:Title Large Crate 2",
        "Cargo Bot:Title Large Crate 3"
    }

    self.spriteIndex = math.random(1, #self.sprites)
end

function Box:draw()

    --self.body.linearVelocity = vec2(50, 0)

    pushMatrix()
    translate(self.body.x, self.body.y)
    rotate(self.body.angle)
    --self.mesh.draw(self.mesh)
    sprite(self.sprites[self.spriteIndex], 0, 0, 100, 100)
    popMatrix()
end

function Box:touched(touch)

end

--# Ground
Ground = class()

function Ground:init(x, y, size)
    self.body = physics.body(POLYGON, vec2(-size, -10), vec2(size, -10), vec2(size, 10), vec2(-size, 10))
    self.body.type = KINEMATIC
    self.body.interpolate = true
    self.body.x = x
    self.body.y = y

    self.body.friction = 0.5

    self.mesh = mesh()
    self.mesh.vertices = triangulate(self.body.points)
    self.mesh:setColors(255,255,0,255)
end

function Ground:draw()

    pushMatrix()
    translate(self.body.x, self.body.y)
    rotate(self.body.angle)
    --self.mesh.draw(self.mesh)
    sprite("Cargo Bot:Game Area Floor", 0, 0, WIDTH, 20)
    popMatrix()
end

function Ground:touched(touch)

end

Comments

  • Posts: 1,139

    Hi @Dominik
    Haven't tried your code but this routine in the Draw function looks like infinite table:


    if ElapsedTime - time > 0.2 then table.insert(boxes, Box(200 + math.random(200), HEIGHT + 200, 50)) time = ElapsedTime end

    If so - bang goes your memory !!!

  • dave1707dave1707 Mod
    Posts: 7,427

    @Dominik Add collectgarbage() as the first line in the draw function. This will take care of memory as you add/delete boxes. After I added that, it ran OK on my iPad.

  • Posts: 808

    @dave1707 memory problem should not crash Codea though.

  • dave1707dave1707 Mod
    Posts: 7,427

    @TokOut A memory problem as a project runs is the main reason Codea crashes.

  • I tend to agree that ideally Codea would terminate the errant script if it was running out of memory rather than just crash, but there could be all sorts of reasons why that might be hard to do in all cases.

  • SimeonSimeon Admin Mod
    Posts: 4,829

    Yeah we should be better about this and find a way to handle projects using large amounts of memory. I'll log this in the issue tracker as a bug.

  • Posts: 18

    Thanks for feedback. But I am sure, that (memory, big amounts) isn’t the problem.. I also build another project with huge amount of physical object. btw. great performance. :). @Bri_G This is not an infinity table. because I always delete boxes when falling out to offscreen.

  • Posts: 18

    @Simeon Could the ipa crash report file of iOS for codea help you?

  • Posts: 18

    Ok, it seems like a memory issue. @dave1707 thanks for the tip. I looks like the method collectgarbage() prevents the problem.
    But it‘s weird. I‘ve used codea before with much more instsnces without problems.

  • Posts: 424

    It is a memory issue, but just shoving in collectgarbage() masks the problem without figuring out why you're hitting the memory limit.

    The problem is that although you're removing your boxes from the table, you aren't destroying them. In particular, the physics bodies that you create don't get cleaned up and the physics engine still takes note of them. Putting in collectgarbage() forces Codea to clean up these because there aren't any references to them.

    But there is another way. Before deleting the box, destroy its physics body.

    for i, box in pairs(boxes) do
         if box.body.y < 0 then
             box.body:destroy()
             table.remove(boxes, i)
         end
    end
    

    Incidentally, the box = nil line does nothing.

    A useful diagnostic is collectgarbage("count") which tells you how much memory lua is currently using. This can show if memory is an issue and help track down what is causing the memory spike.

  • edited October 2017 Posts: 1,139
    @Dominik suggest you try adding the box create to the touch function and limit #Box to a maximum.
  • Posts: 18

    @LoopSpace Thanks, that was the problem. I had overlooked that. I see, in another project (I wrote that also has a lot of physical things) I already did this (body:destroy()) in a custom destroy (dispose) method of the class. Shame on me. :D

    Thanks to all for your constructive feedback.

Sign In or Register to comment.