Trouble with Garbage Collection

-- PhysicsProj

-- Use this function to perform your initial setup
function setup()
    w1 = physics.body(EDGE, vec2(0,0), vec2(WIDTH, 0))
    w2 = physics.body(EDGE, vec2(0,0), vec2(0, WIDTH))
    w3 = physics.body(EDGE, vec2(WIDTH, 0), vec2(WIDTH, HEIGHT))
    linedestroy = true
    balls={}
    balls= CreateBall()
    
end

function CreateBall()
    local p = physics.body(CIRCLE,50)
    p.restitution = .8
    p.linearVelocity = vec2(math.random(100,400), math.random(100,400))
    p.x = math.random(60,250)
    p.y = math.random(400,600)
    p.color = color(math.random(0,255), math.random(0,255), math.random(0,255),100)
    return p
    end

   
    function touched(t)
        if t.state == BEGAN and not tw then
            if CurrentTouch.x < otherx1 and CurrentTouch.x > otherx2 then
                print("Success! Detected physics object.")
            else
                if linedestroy == true then
                print("Touch not in area of physics object. Initializing garbage collection.")
                w1:destroy()
                linedestroy = false
                end
                end
        if t.state == ENDED then
                p:destroy()
                p = nil
                
                end
        
        end
end

function draw()
    -- This sets a dark background color
    background(220)
    pushStyle()
    fill(255,255,0)
        fill (balls.color)
        ellipse(balls.x, balls.y, 100)
        
    popStyle()
    otherx1 = balls.x + 45
    otherx2 = balls.x - 45
    

    -- This sets the line thickness
    strokeWidth(5)

    -- Do your drawing here
    
end

Hey guys. I’ve been having trouble with the :destroy() recently. I’ve been screwing with physics and I wanted to test out garbage collection with simple shapes. I do p:destroy(), while p is a physics body, call it, nothing happens. End the touch, the ball still appears as it sinks towards the bottom.
(The code is at the end of the touch function.) Help?
(No errors.)

@Treshure - you are destroying the wrong thing. Try destroying balls (and setting it to nil) instead.

Then in draw, you need to test if balls exists before trying to draw it.

I tried balls as well, no different results. I also added:

if balls ~= nil then

as well as it’s opposite, printing an error message, but still no results.

@Treshure - your problem is that the line “if t.state==ENDED …” Is inside the if statement that tests for t.state=BEGAN

It should not be.

If you indent all your end statements carefully, you’ll see this.

@Treshure - I use a separate class to capture and delete physics objects (which I’m pretty sure I found somewhere on this forum :-)). Check out the code below:

DeleteObjects = class()

function DeleteObjects:init()
       self.bodies = {}
end

function DeleteObjects:addBody(body)
       table.insert(self.bodies,body)
end

function DeleteObjects:clear()
       for i,body in ipairs(self.bodies) do
             body:destroy()
             body = nil
       end
       self.bodies = {}
end

When I create the physics object I use the add body function to add the object to the self.bodies table. Using your example, each time I create a ball I would add this line:

DeleteObjects:addBody(balls)

When I’m ready to get rid of the objects I use the line:

DeleteObjects:clear()

This will delete all of the physics objects held in the table self.bodies.

Let me know how you go.