Memory Leak

I’m writing a small game using Corona for my game design class and have been working with corona for just under 2 weeks now. I’ve learned a lot of the basics, but I’m still very new.

Up to now, I’ve been able to find and remedy the memory problems, but now it’s just not working. It slows down after about 2 minutes of gameplay.

http://www.mediafire.com/?5fz7ru0c6euwq1k

This is my project folder so far. If anyone can help me find the source of the memory leak, I’d be very appreciative. [import]uid: 89724 topic_id: 15588 reply_id: 315588[/import]

As one example your bullet list grows steadily - you print “removed wheeeeeeeeeeeeeeeeeeeeeee” about bullets, but they are only invisible not really removed.

Think about “recycling” of objects. You don’t need an unlimited number of bullets. If a bullet is out of sight, you can use it again when you need the next bullet. Just place it and make it visible. On start prepare the maximum needed number of bullets.

Maybe there are more holes… [import]uid: 70114 topic_id: 15588 reply_id: 57563[/import]

That notification looks and acts that way because it removed them at first, but the removal code threw an error, so I changed it to the invisible/inactive code. The long print message was to let me see if it was being called and to differentiate it from the rest of the print notifications.

Do you have any suggestions for the proper way to remove the bullets from that method?

Also I had considered recycling, but had run into some difficulties with it. The difficulties were due mostly to my lack of experience at the time, so I may be able to integrate recycling now. However, for future reference, I was hoping to find out what I’d missed in this iteration of the code. [import]uid: 89724 topic_id: 15588 reply_id: 57600[/import]

Then you can have the object it is colliding with ( i.e. the kill zone walls ) call the objects destroy method.

[code]

– Collision Handler
local function onLocalCollision( self , event )

– On Collision
if ( event.phase == “began” ) then

– If Hit Function Then Destroy
if event.other.destroy then
event.other.destroy()
end

end

end
[/code] [import]uid: 5354 topic_id: 15588 reply_id: 57610[/import]

Codes a bit mad…

Few helpful hints though…

  1. Draw 4 rectangles outside of the viewable area and use those to detect and kill any bullets thats go off screen. Use collision tables so only the bullets and the walls interact as otherwise it would stop other objects passing through the walls.

  2. Construct your objects so that do not rely on loops and functions where you don’t need to. For instance the bullet should contain all the code that controls the bullet.

The idea here is you create a standard Corona object, pass it to the physics engine and ignore it until it triggers an event.

Heres a non tested example

[code]

– New Bullet
local function newBullet()

– New Bullet
local bullet = display.newImageRect()

– Physics Settings
local collisionFilter = { categoryBits = 4 , maskBits = 9 }
local physicsSettings = { density = 1 , friction = 1 , bounce = 1 , filter = collisionFilter }

– Add Physics
physics.addBody( bullet , physicsSettings )

– Is Hit Function
bullet.isHit = function()

– Play Explosion
playExplosion( bullet.x , bullet.y )

– Remove Physics
bullet:removeSelf()

– Cleanup
bullet = nil

end

– Destroy Function
bullet.destroy = function()

– Remove Physics
bullet:removeSelf()

– Cleanup
bullet = nil

end

– Return Bullet
return bullet

end
[/code] [import]uid: 5354 topic_id: 15588 reply_id: 57609[/import]

Yes, I don’t usually code with readability in mind. My apologies.

I had no idea you could define methods for each object. There’s my inexperience coming into play again. Thanks for the help. Could you please tell me what a collision table is and how to use it? [import]uid: 89724 topic_id: 15588 reply_id: 57635[/import]

Api - http://developer.anscamobile.com/content/game-edition-collision-detection#Collision_categories_masking_and_groups

Nice table to download - http://developer.anscamobile.com/forum/2010/10/25/collision-filters-helper-chart [import]uid: 5354 topic_id: 15588 reply_id: 57644[/import]

Thanks again for your help, but now I have another issue.

I’ve cleaned the code considerably and expanded it even more so in the past 2 weeks. Now I’m running into a similar, but not quite as intense, memory problem. I think I may be reaching the limit of unique onscreen sprites at a time the simulator allows, but I’d like to exhaust my options before I start cutting things from the game.

My first question: does the practice of re-declaring the same variable in different methods effect memory in a noticeable way?

Second: Would eliminating needless collisions via collision tables reduce memory consumption?

Third: Is there some sort of memory consumption property I can monitor from the console to see which methods save power and which do not?

Fourth: Besides removing objects when they’ve outlived their usefulness, disposing of sprite sheets when they’re no longer needed, and limiting the amount of onscreen objects, what other memory saving tricks can I use to keep the speed constant?

I can post my app if you like, but it is somewhat tangled as my grasp of Lua is still tenuous.

Thanks in advance. [import]uid: 89724 topic_id: 15588 reply_id: 60165[/import]