Program crashing, any ideas?

Here’s some pretty rough code I’ve been using to learn. I’m having a problem with the application locking up codea or dropping me to the desktop when I uncomment line 50 “bullet2=physics.body(CIRCLE,ballsize)”. Line 50 is creating a physics body.The app is a little shooter. You swipe your finger below the yellow line and a bullet travels according to the swipe direction and length. Line 50 isn’t run until you shoot a green orb. Shortly thereafter I’m getting the crash. The dblfire section is suppose to allow the shooting of two bullets simultaneously. Any debugging direction would be appreciated.

-- Use this function to perform your initial setup
function setup()
   -- displayMode(FULLSCREEN)
    physics.gravity(vec2(0,-10))
    bullets={}
    enemies={}
    spenemies={}
    activeline=false
    parameter("Speed",0,1000,200)
    ballsize=10
    balldensity=2
    totaltime=0
    score=0
    bulletcost= 1
    watch("DeltaTime")
    watch("table.maxn(enemies)")
    fireline=HEIGHT/3
    dblfire=false
    numstars=200
    starspeed=5
    stars={}
    watch("DeltaTime")
    for i=1, numstars do
        s=math.random(starspeed)
        stars[i]={x=math.random(WIDTH),y= math.random(HEIGHT), speed=s, starbright=s*30}
    end
end

function touched(touch)
    if CurrentTouch.state == BEGAN and CurrentTouch.y < fireline then
        activeline = true
        startx=CurrentTouch.x
        starty=CurrentTouch.y
        startvec=vec2(startx,starty)
    elseif CurrentTouch.state== MOVING then
 --       totaltime = totaltime + DeltaTime
    elseif CurrentTouch.state == ENDED and activeline then
        curx=CurrentTouch.x
        cury=CurrentTouch.y
        curvec=vec2(curx,cury)
        bullet=physics.body(CIRCLE,ballsize)
        bullet.x=startx
        bullet.y=starty
        bullet.bullet=true
        bullet.density=balldensity
        force=startvec-curvec
        bullet:applyForce(force*10)
        table.insert(bullets,bullet)
        if dblfire then
           bullet2=physics.body(CIRCLE,ballsize)
         --   bullet2.x=startx-15
          --  bullet2.y=starty
         --   bullet.bullet=true
         --   bullet2.density=balldensity
         --   force=startvec-curvec
         --   bullet2:applyForce(force*10)
         --   table.insert(bullets,bullet2)      
        end
        activeline=false
   --     totaltime = 0
        score = score - bulletcost
        sound(SOUND_SHOOT, 6756)
    end
end  -- shoot

function collide(contact)
    if contact.state == BEGAN then
        a=contact.bodyA
        b=contact.bodyB
        for i,c in pairs(enemies) do
           if c==a or c==b then
              sound(SOUND_RANDOM, 32780)
              table.remove(enemies,i)
              score = score + 10
           end
        end
        for i,c in pairs(spenemies) do
           if c==a or c==b then
              sound(SOUND_RANDOM, 32780)
              table.remove(spenemies,i)
              score = score + 10
              dblfire= true
        end
        end
    end
end

function drawline()
    if activeline then 
        pushStyle()
        stroke(255, 0, 0, 255)
        strokeWidth(6)
        line(startx,starty,CurrentTouch.x,CurrentTouch.y)
        popStyle()
    end
end -- drawline

function drawenemy()
    pushStyle()
    fill(255, 0, 0, 255)
    for i,e in pairs(enemies) do
        if e.y < 0 then
            table.remove(enemies,i)
        else
        sprite("Tyrian Remastered:Fire Rock",e.x,e.y,e.radius*2)
        end
    end
    for i,e in pairs(spenemies) do
        sprite("Tyrian Remastered:Evil Orb",e.x,e.y,30)
    end
    
    popStyle()
end -- drawenemy

function drawbullets()
    pushStyle()
    for i,b in pairs(bullets) do
        if b.x < 0 or b.x > WIDTH or b.y < 0 or b.y > HEIGHT then
            table.remove(bullets,i)
        else
          sprite("Tyrian Remastered:Bullet Energy A",b.x,b.y,b.radius*1)
        end
    end
    popStyle()
end

function checkfornewenemy()
    if math.random(35)==1 then
        enemy=physics.body(CIRCLE,3+math.random(4,ballsize))
        enemy.x=math.random(WIDTH)
        enemy.y=HEIGHT
        enemy.bullet=true
        enemy:applyForce(vec2(0,-math.random(enemy.radius*3)))
        table.insert(enemies,enemy)
    end
end

function drawfireline()
    pushStyle()
    stroke(255, 253, 0, 255)
    strokeWidth(4)
    line(0,fireline,WIDTH,fireline)
    popStyle()
end

function adjustenemyrate()
    totaltime = totaltime + DeltaTime
end

function drawscore()
    textSize(60)
    font("AmericanTypewriter")
    fontSize(60)
    text(score, WIDTH/2, HEIGHT-100)
end


function drawstars()
    for i, numstars in pairs(stars) do
        fill(255, 255, 255, stars[i].starbright)
        ellipse(stars[i].x, stars[i].y,1+math.random(4))
        stars[i].y = stars[i].y - stars[i].speed
        if stars[i].y < 0 then 
           stars[i].y = HEIGHT
           stars[i].x = math.random(WIDTH)
        end
    end
end

function checkforspecialenemy()
    if math.random(400)==1 then
        spenemy=physics.body(CIRCLE,20)
        spenemy.x=math.random(WIDTH)
        spenemy.y=HEIGHT-(HEIGHT/10)*math.random(3)
        table.insert(spenemies,spenemy)
    end
end

function drawdbltimer()
    if autofire then 
    pushStyle()
    strokeWidth(10)
    stroke(108, 255, 0, 255)
    line(WIDTH-20,HEIGHT- 25,WIDTH-20,HEIGHT-25-dbltimer)
    dbltimer = dbltimer - 1
    popStyle()
    end
end

function draw()
    pushStyle()
    background(0, 0, 0, 0)
    drawstars()
    --adjustenemyrate()
    drawfireline()
  --  drawline()
    checkfornewenemy()
    checkforspecialenemy()
    drawenemy()
    drawbullets()
    drawscore()
    popStyle()
end


Hi @Marksolberg

I see your creating a lot of bullet/enemy physics.body, and inserting them into a table. When the bullet leaves the screen, or the enemy is hit, you’re removing them from the table. Even though you’re removing them from the table, they still exist and take up memory. You need to destroy the object. See body.destroy in the reference. I don’t know if that’s the problem or not, but you should still destroy them.