Memory leak when spawning, can anyone see whats wrong?

Hi,

I have some code in my game which spawns a car at a specified interval, the memory usage seems to creep up though even if its just the same event happening again and again.

Can anyone see a better way to perform the code?

Thanks much appreciated.

[blockcode]
local function trafficRight()

local function resetSpawn()
dospawnRight = 1
end
local function spawncar()
local cartype = 0
local chooseLane = math.random(3,4)
dospawnRight = 0
timer.performWithDelay(delayRight, resetSpawn, 1)
if level > 0 and level < 3 then
cartype = 1
elseif level == 3 then
cartype = 5
elseif level > 3 then
cartype = math.random(1,3)
end
if cartype == 1 then
local cpucar = display.newImageRect(settings.miniGreen, settings.miniGreen_H, settings.miniGreen_W)
physics.addBody(cpucar, “kinematic”, {isSensor = true })
if chooseLane == 3 then
cpucar.x = lane3x
else cpucar.x = lane4x
end
cpucar.dead = 0
cpucar.y = -50
cpucar.rotation = 90
cpucar.collision = onCollision
Runtime:addEventListener(“enterFrame”, cpucar )
cpucar:addEventListener(“collision”, cpucar )
function cpucar:enterFrame(event)
if cpucar.y > 480 then
cpucar.dead = 1
end
if pause == 0 then
if cpucar.dead == 1 then
Runtime:removeEventListener(“enterFrame”, cpucar)
cpucar:removeEventListener(“collision”, cpucar)
cpucar:removeSelf()

elseif cpucar.dead == 0 then
cpucar:setLinearVelocity(0,settings.gamespeed + math.random(60,80) )
end
else cpucar:setLinearVelocity(0,0)
end
end
end
end

if pause == 0 and level > 0 then
if dospawnRight == 1 then
spawncar()
end
end
end

timer.performWithDelay(100, trafficRight, 0 )
[/blockcode]

The full code has options for 2 more car types, but its the same code as above. [import]uid: 8699 topic_id: 6261 reply_id: 306261[/import]

physics.addbody had mem leak bug that has been fixed in the latest build.

C [import]uid: 24 topic_id: 6261 reply_id: 21642[/import]

Thanks for the reply Carlos. Is it fixed in the latest released build or an upcoming release? I’m pretty sure I have the latest released which was from 2 weeks or so ago when the windows support was announced.

Thanks [import]uid: 8699 topic_id: 6261 reply_id: 21688[/import]

I managed to reduce the amount of timers and collision detections I had, this seems to have improved performance quite a lot. The player car now detects the collision rather than the spawned cars.

Also letting the corona engine collect the garbage when it thinks it should, rather than forcing it myself seems to have helped. [import]uid: 8699 topic_id: 6261 reply_id: 21833[/import]