[SOLVED] How to clear tables?

Hi all,

I’m having a memory leak problem, and i can’t figure out how to fix it. I think it has to do with tables.

I’m checking my memory at regular points to keep an eye on it, and found out that Texture memory is stable, all the objects/sprites are released as soon as i reload a level, but memory usage increases.

In a nutshell, i load a level with objects, and create a table to handle and assign properties:

-- level.lua  
local platform = {}  
  
loader = LevelHelperLoader:initWithContentOfFile("level" .. Level .. ".plhs")  
loader:enableRetina(false)  
loader:instantiateObjectsInGroup(physics, localGroup)  
local allSpr = loader:allSprites();  
  
for k, v in pairs (allSpr) do  
  
 if 1 == v.tag then -- Blue  
 NumberOfPlatforms = NumberOfPlatforms + 1  
 platform[NumberOfPlatforms] = v.coronaSprite  
 platform[NumberOfPlatforms].Type = "Blue"  
 platform[NumberOfPlatforms].isDown = false  
 platform[NumberOfPlatforms].numFrames = v.coronaSprite.numberOfFrames  
 platform[NumberOfPlatforms].curFrame = 1  
 NumberOfBlueBoxes = NumberOfBlueBoxes + 1  
 print("Blue Box")   
 platform[NumberOfPlatforms]:addEventListener( "touch", RemovePlatform )  
 end  
  
 if 2 == v.tag then -- Green  
 NumberOfPlatforms = NumberOfPlatforms + 1  
 platform[NumberOfPlatforms] = v.coronaSprite  
 platform[NumberOfPlatforms].Type = "Green"  
 platform[NumberOfPlatforms].isDown = false  
 platform[NumberOfPlatforms].numFrames = v.coronaSprite.numberOfFrames  
 print("Green Box")  
 platform[NumberOfPlatforms].curFrame = 1   
 end  
  
 if 3 == v.tag then -- Red  
 NumberOfPlatforms = NumberOfPlatforms + 1  
 platform[NumberOfPlatforms] = v.coronaSprite  
 platform[NumberOfPlatforms].Type = "Red"   
 NumberOfRedBoxes = NumberOfRedBoxes + 1  
 platform[NumberOfPlatforms].isDown = false  
 platform[NumberOfPlatforms].numFrames = v.coronaSprite.numberOfFrames  
 platform[NumberOfPlatforms].curFrame = 1  
 print("Red Box")  
 --print (string.format("%s", platform[NumberOfPlatforms].y))  
 end  
end  

Before i reload the level (switching to loading.lua, who loads level.lua again) i cleanup every object, timer, eventListener.
The texture Memory is stable, so all texture objects a freed from memory. But i think the table 'platform’is causing the problem.

I tried cleaning up the table with platform = nil, but that gives me even more errors.

Is there any method to empty a table and its contents from memory?

I tried to declare the table like:

platform = {}

instead of

local platform = {}

and that really seems to help, but again, it gives me lots of NIL value errors after loading the level. So im totally confused.

[import]uid: 50459 topic_id: 13119 reply_id: 313119[/import]

There are two things that may help you :slight_smile:

First this thread; http://developer.anscamobile.com/forum/2011/03/01/removing-table-full-images which talks about a table containing images but has a VERY useful answer.

Second is this API, to better understand the above thread if it is unclear to you :slight_smile: http://developer.anscamobile.com/reference/index/tableremove

Peach :slight_smile:

PS - This piece by Jon Beebe is GREAT for learning about tables and has some comments at the bottom about how to use [lua]nil[/lua] correctly; http://blog.anscamobile.com/2011/06/understanding-lua-tables-in-corona-sdk/ [import]uid: 52491 topic_id: 13119 reply_id: 48178[/import]

peach you just made my day :slight_smile:

Thank you so much. I will read the other articles/posts too to get a better understanding of tables!

Have a great day! [import]uid: 50459 topic_id: 13119 reply_id: 48184[/import]