[Resolved] Can't solve a memory leak.

I have a game where I am firing a gun. Each time the gun is fired I call the functions “newBullet” and then “Bullet:fire”

I feel like i am cleaning everything up fine, but every time I fire my memory usage goes up.
The clean up is in the collisions function.
I have a wall around the game screen so that the bullet will ALWAYS collide, and is never infinitely flying in space.

Any help would be greatly appreciated!! :smiley:

[code]

–Bullet

–REQUIRED–
local print = print
local transition = transition
local physics = physics
local math = math
local timer = timer
local display = display
local require = require
local Runtime = Runtime

module(…)

–INCLUSIONS–
local Engine = require(“Engine”)

–PROPERTIES–
local bulletFilter = {categoryBits = 4, maskBits = 3}

–GROUPS/TABLES–
local resourceGroup = display.newGroup()

–BULLET OBJECT
function newBullet()
local Bullet = display.newImage(Engine.getCurrentGun() … “_bullet.png”)
–local Bullet = display.newImage(“defaultBullet.png”)
Bullet.xScale = Engine.getScale()
Bullet.yScale = Engine.getScale()
local size = (5*Engine.getScale())
physics.addBody ( Bullet, “dynamic”, {density=1.0, friction=1.0, bounce=1.0, radius=size, filter=bulletFilter})
Bullet.collision = bulletCollisions
Bullet:addEventListener(“collision”, Bullet)

–PROPERTIES
Bullet.x = display.contentWidth/2
Bullet.y = display.contentHeight/2
Bullet.name = Engine.getCurrentGun()

Bullet.damage = 1
if(Bullet.name == ‘fmj’)then Bullet.damage = 1 Bullet.speed = 1200 * Engine.getScale()
elseif(Bullet.name == ‘shotgun’)then Bullet.damage = 1 Bullet.speed = 1500 * Engine.getScale()
elseif(Bullet.name == ‘laser’)then Bullet.damage = 5 Bullet.speed = 2000 * Engine.getScale()
elseif(Bullet.name == ‘glauncher’)then Bullet.damage = 10 Bullet.speed = 500 * Engine.getScale()
elseif(Bullet.name == ‘laser’)then
elseif(Bullet.name == ‘laser’)then
elseif(Bullet.name == ‘laser’)then
end

–FUNCTIONS
function Bullet:fire(event)
local xTemp = event.x - display.contentWidth/2
local yTemp = event.y - display.contentHeight/2
local newAngle = math.atan2 ( yTemp, xTemp)*180/math.pi
–shotgun bullet spread
if(Bullet.name == ‘shotgun’)then newAngle = newAngle + math.random(-15,15) end
local xVelocity = Bullet.speed * math.cos(newAngle*3.14/180)
local yVelocity = Bullet.speed * math.sin(newAngle*3.14/180)

Bullet.rotation = newAngle
Bullet:setLinearVelocity( xVelocity, yVelocity)

Engine.setAmmuntion(-1)
end

function bulletCollisions(self, event)
if(event.phase == “began”) then
if(Bullet.name == ‘fmj’)then
display.remove(Bullet)
Bullet = nil
elseif(Bullet.name == ‘shotgun’)then
display.remove(Bullet)
Bullet = nil
elseif(Bullet.name == ‘laser’)then
if(event.other.enemyName == ‘heavy’)then display.remove(Bullet) Bullet = nil end
elseif(Bullet.name == ‘glauncher’)then
–Actual explosion physics object
local function makeExplosion()
local explosion = display.newImage(“explosion.png”)
explosion.x = self.x
explosion.y = self.y
physics.addBody ( explosion, “dynamic”, {density=1.0, friction=1.0, bounce=1.0, radius=(75*Engine.getScale()), filter=bulletFilter})
explosion.damage = 5
explosion.isSensor = true
Bullet:removeSelf()
Bullet = nil
local function killExplosion()
display.remove(explosion)
explosion = nil
end
timer.performWithDelay(1,killExplosion,1)
end
timer.performWithDelay(1,makeExplosion,1)
elseif(Bullet.name == ‘pigeon’)then
elseif(Bullet.name == ‘someotherweapon’)then
elseif(Bullet.name == ‘the good thing is, hes dead and im alive’)then
end
end
end

return Bullet
end
function removeEnemies()
–Engine.emptyGroup(resourceGroup)
display.remove(resourceGroup)
end

–########
`` [import]uid: 30414 topic_id: 31160 reply_id: 331160[/import]

Are you 100 percent sure your collision event is firing for every single bullet? Since bullets move fast you can get what’s called ‘tunneling’. Basically it’s where your fast moving object(like a bullet) moves over an object in between frames/cycles/whatever. When this happens no collision event is fired. Luckily the people who developed Box2d thought of this and there is a ‘isBullet’ property you can set on your, well, bullets. This usually takes care of tunneling issues.

I believe it’s is simple as object.isBullet = true but may want to check the docs just in case. If this doesn’t fix your issue then I would institute a way of keeping track of how many bullets are currently in play. This could let you know if you have some leaky bullets around. A simple counter on spawn and another on remove should do it. [import]uid: 147305 topic_id: 31160 reply_id: 124632[/import]

I’ve dealt with this problem before. Don’t know why I didn’t just track all my bullets… haha

Well I implemented a simple counter and found that a TON of bullets were getting through my game bounds. Ended up making the game bound was EXTREMELY thick and that fixed the problem right up!

Thanks for the help! Sometimes I just need another person to tell me what I probably should have already thought of haha.

Thanks again!

Happy coding! [import]uid: 30414 topic_id: 31160 reply_id: 124641[/import]

Are you 100 percent sure your collision event is firing for every single bullet? Since bullets move fast you can get what’s called ‘tunneling’. Basically it’s where your fast moving object(like a bullet) moves over an object in between frames/cycles/whatever. When this happens no collision event is fired. Luckily the people who developed Box2d thought of this and there is a ‘isBullet’ property you can set on your, well, bullets. This usually takes care of tunneling issues.

I believe it’s is simple as object.isBullet = true but may want to check the docs just in case. If this doesn’t fix your issue then I would institute a way of keeping track of how many bullets are currently in play. This could let you know if you have some leaky bullets around. A simple counter on spawn and another on remove should do it. [import]uid: 147305 topic_id: 31160 reply_id: 124632[/import]

I’ve dealt with this problem before. Don’t know why I didn’t just track all my bullets… haha

Well I implemented a simple counter and found that a TON of bullets were getting through my game bounds. Ended up making the game bound was EXTREMELY thick and that fixed the problem right up!

Thanks for the help! Sometimes I just need another person to tell me what I probably should have already thought of haha.

Thanks again!

Happy coding! [import]uid: 30414 topic_id: 31160 reply_id: 124641[/import]