I get a memory leak when trying to remove objects. I’ve determined that it happens only when each of the objects are given physics bodies, which I thought I was removing correctly according to the documentation. My code:
[code]
local physics = require(“physics”)
physics.start()
physics.setGravity( 0, 0 )
local score = 0
local x = 25
local background = display.newImage(“background.png”)
scoreTextfield = display.newText( " " … score, 280, 105, nil, 14 )
scoreTextfield:setTextColor( 0, 0, 0, 255 )
scoreTextfield.text = " " … score
collectgarbage(“collect”)
score = "System Memory : " … collectgarbage(“count”)
scoreTextfield.text = " " … score
local bar = display.newImage( “bar.png” )
bar.x = 170 ; bar.y = 350
physics.addBody( bar, “static”, { density=2.9, friction=0.0, bounce = 0 } )
local function randomBall ()
if x < 3000 then
local imageBall = display.newImage( “ball.png” )
local randomPos = math.random
imageBall.x = 10 + randomPos( 300 ); imageBall.y = -20
physics.addBody( imageBall, { density=2.9, friction=0.0, bounce = 0 } )
imageBall:setLinearVelocity( 0, 600 )
imageBall:addEventListener( “collision”, imageBall )
collectgarbage(“collect”)
score = "System Memory : " … collectgarbage(“count”)
scoreTextfield.text = " " … score
local function onLocalCollision( self, event )
if ( imageBall.x ) then
if ( imageBall.y > 300 ) then
imageBall:removeEventListener( “collision”, imageBall )
imageBall:removeSelf()
imageBall = nil
collectgarbage(“collect”)
score = "System Memory : " … collectgarbage(“count”)
scoreTextfield.text = " " … score
end
end
end
imageBall.collision = onLocalCollision
end
end
y = 30
local timberBall = timer.performWithDelay( x, randomBall, y )
[/code] [import]uid: 10903 topic_id: 3988 reply_id: 303988[/import]