[Resolved] group cleaning question

I do the following to clean my groups :

for i=mygroup.numChildren,1,-1 do  
 local child = mygroup[i]  
 display.remove(child)  
 child=nil  
end  
display.remove(mygroup)  
mygroup=nil  

I was wondering : what if mygroup has other groups nested in (with eventlisteners) each of them having some images ?

My code cleans mygroup, any groups inserted in and objects inserted in the inserted groups (ouch) ?

Thx [import]uid: 9328 topic_id: 27083 reply_id: 327083[/import]

I believe this should work fine however for a quick test simply check the memory usage before and after you call this function.

Do you need code for that? Let me know if so, will dig up sample :slight_smile: [import]uid: 52491 topic_id: 27083 reply_id: 109977[/import]

Thx for the reply Peach. I do have some way to track memory usage, but answers are not always obvious.
Glad to hear my code is ok. [import]uid: 9328 topic_id: 27083 reply_id: 110032[/import]

Here’s some very rough code but it shows output that may be useful; if you run it and just replace “smile.png” with your own image you’ll see results.
[lua]local function monitorMem(event)
collectgarbage(“collect”)

print( “\nMemUsage: " … (collectgarbage(“count”)/1000) … " MB”)
print("Texture Usage " … system.getInfo( “textureMemoryUsed” ) / 1000000)

return true
end
monitorMem()

local mainGroup = display.newGroup()
local group1 = display.newGroup()
local obj = {}

for i = 1, 10 do
obj[i] = display.newImage(“smile.png”)
obj[i].x, obj[i].y = math.random(20,300), math.random(20,460)
group1:insert(obj[i])
end

mainGroup:insert(group1)

monitorMem()

local function cleanUp()
for i=mainGroup.numChildren,1,-1 do
local child = mainGroup[i]
display.remove(child)
child = nil
end
display.remove(mainGroup)
mainGroup = nil
end
timer.performWithDelay(500, cleanUp, 1)

timer.performWithDelay(1000, monitorMem, 1)[/lua]

(Just posting that in case you are curious. It’s not that organized but should show you what I mean.)

Peach :slight_smile: [import]uid: 52491 topic_id: 27083 reply_id: 110086[/import]

Thx again. [import]uid: 9328 topic_id: 27083 reply_id: 110107[/import]

Hi,

Sorry to post in a resolved thread but I find the code confusing.

Your looping through mainGroup and removing all the children, one of the children is group1.

You are not going through group1 and removing all children, is removing group1 and setting it to nil enough ?

If the answer is yes then why not just do -

display.remove(mainGroup)
mainGroup = nil

As that is exactly what you are doing to group1.

Dave [import]uid: 117617 topic_id: 27083 reply_id: 110432[/import]

I’m not an expert but you have loop through child items of a group for a proper cleaning of objects (and listeners attached to them)

display.remove(mainGroup);mainGroup = nil
is not enough. [import]uid: 9328 topic_id: 27083 reply_id: 110452[/import]