Will this sample small code have memory leak?

Hi there,

With this sample code, will there be a memory leak on the “bullet” variable, assuming the player taps the screen in rapid succession so that there are more than one “bullet” seen on screen at a given time?  

\_W = display.contentWidth \_H = display.contentHeight physics = require("physics") physics:start() physics.setGravity(0,0) local barrier = display.newRect(0,10,\_W,15) barrier.x = \_W/2 physics.addBody(barrier, "static", {density=0.2, friction=0.2, bounce=0.2}) local bullet ------------------------------------------------- local function onBarrierCollision( self, event ) if event.other.name == "bullet" then event.other:removeSelf() event.other = nil print("bullet removed..." ) end end barrier.collision = onBarrierCollision barrier:addEventListener("collision", barrier ) ----------------------------- local function gunner(event) if event.phase == "began" then bullet = display.newCircle(\_W/2, \_H-50, 5) bullet.x = \_W/2 bullet.y = \_H-50 bullet.name = "bullet" physics.addBody(bullet, "dynamic", {density=1.5, bounce=0}) bullet:applyLinearImpulse(0, -2) end end Runtime:addEventListener( "touch", gunner )

Your comments will be highly appreciated.

Thanks!

On first look it shouldn’t but almost certainly you will have problem because you cannot remove object in collision handler function. You must delay removal proces with eg. performwithdelay with at least 1 ms.

But really encourage you to make physics, _H, _W etc. local variables. Globals are no-no

Removing a physics object upon collision should not require a 1ms or greater delay. Some physics actions require a short delay, but simple removal isn’t one of them.

And yes, as @piotrz55 says, globals are bad… and so easy to avoid using. :slight_smile:

Brent

@Brent, wouldn’t removing object during its collision mess somehow engine? I am positiv it was said you need some delay when doing such action.

Hi @piotrz55,

Destroying/removing objects in a collision isn’t one of those actions which requires a timer delay. Other actions do, however, like trying to modify an object’s x or y position, change its body status from active to inactive, etc. I don’t have a full list of which property changes require a delay, but conveniently, removal is not one of them.

Brent

Thanks guys.   Yes, I was aware of the globals   :slight_smile:   The code was typed so hastily just to illustrate my question, so I left out the all-important locals declarations   :wink:

I have another question though… My bullets and the enemy’s bullets collide. Since they are all “dynamic”, they react physically, like bounce off from each other upon impact.  Is there a way I can disable collision (let the bullets fly through each other) between enemy and player bullets but still detect collision on their respective targets; in this case, enemy bullets on player, player bullets on enemy, but not player vs enemy bullets. 

Thanks in advance,

Yes, use filter property of physics object (see docs)

Great!  I’ve found this very detailed info about filters but unfortunately the PDF chart link is broken. 

http://developer.coronalabs.com/forum/2010/10/25/collision-filters-helper-chart

On first look it shouldn’t but almost certainly you will have problem because you cannot remove object in collision handler function. You must delay removal proces with eg. performwithdelay with at least 1 ms.

But really encourage you to make physics, _H, _W etc. local variables. Globals are no-no

Removing a physics object upon collision should not require a 1ms or greater delay. Some physics actions require a short delay, but simple removal isn’t one of them.

And yes, as @piotrz55 says, globals are bad… and so easy to avoid using. :slight_smile:

Brent

@Brent, wouldn’t removing object during its collision mess somehow engine? I am positiv it was said you need some delay when doing such action.

Hi @piotrz55,

Destroying/removing objects in a collision isn’t one of those actions which requires a timer delay. Other actions do, however, like trying to modify an object’s x or y position, change its body status from active to inactive, etc. I don’t have a full list of which property changes require a delay, but conveniently, removal is not one of them.

Brent

Thanks guys.   Yes, I was aware of the globals   :slight_smile:   The code was typed so hastily just to illustrate my question, so I left out the all-important locals declarations   :wink:

I have another question though… My bullets and the enemy’s bullets collide. Since they are all “dynamic”, they react physically, like bounce off from each other upon impact.  Is there a way I can disable collision (let the bullets fly through each other) between enemy and player bullets but still detect collision on their respective targets; in this case, enemy bullets on player, player bullets on enemy, but not player vs enemy bullets. 

Thanks in advance,

Yes, use filter property of physics object (see docs)

Great!  I’ve found this very detailed info about filters but unfortunately the PDF chart link is broken. 

http://developer.coronalabs.com/forum/2010/10/25/collision-filters-helper-chart