Hi guys,
Is
display.remove (myGroup)
myGroup = nil
same as:
for i = 1, myGroup.numChildren do
display.remove (myGroup[i])
myGroup[i] = nil
end
Or I will have memory leak if I do not tie each child to nil (in first case)?
Many thanks!
Ivan
Hi guys,
Is
display.remove (myGroup)
myGroup = nil
same as:
for i = 1, myGroup.numChildren do
display.remove (myGroup[i])
myGroup[i] = nil
end
Or I will have memory leak if I do not tie each child to nil (in first case)?
Many thanks!
Ivan
Answer - As long as there are no external references to the objects, simply removing the group and nill-ing any references to the group will be leak-less:
local myGroup = display.newGroup() ... then later display.remove (myGroup) myGroup = nil
Quick FYI, you don’t need to assign nil in the second snippet:
for i = 1, myGroup.numChildren do display.remove (myGroup[i]) end
Please remember to format all code posts.

Sorry for not formatting it (it is not easily done while on mobile version).
Can you name one/two examples for external references to the objects ?
Thanks!
I mean if you have other variables referencing the objects you’re deleting then the objects won’t be garbage collected till those variables are cleared or fall out of scope.
I simply could not assume (in my answer) that you only put the objects in a display group. I didn’t know if you’d also used variables somewhere to refer to specific objects in that group.
Can you give one simple example?
Word variable is not so clear…
local group = display.newGroup() display.newCircle( group, 10, 10, 10 ) -- 1 local tmp = display.newCircle( group, 10, 10, 10 ) -- 2 display.newCircle( group, 10, 10, 10 ) -- 3 display.remove( group ) group = nil -- Circle 1 and 3 are removed and will be garbage collected. -- Circle 2 (referenced by variable 'tmp') is removed, but will not be -- garbage collected till 'tmp' is nilled or falls out of scope
Got you!
I almost always do:
local tmp = display.newCircle( group, 10, 10, 10 ) -- 2
It is a habit.
Last question.
Why nil is not required here (as you mentioned above).
I saw so many people doing nil here:
for i = 1, myGroup.numChildren do display.remove (myGroup[i]) end
Thanks Ed! 
Answer - As long as there are no external references to the objects, simply removing the group and nill-ing any references to the group will be leak-less:
local myGroup = display.newGroup() ... then later display.remove (myGroup) myGroup = nil
Quick FYI, you don’t need to assign nil in the second snippet:
for i = 1, myGroup.numChildren do display.remove (myGroup[i]) end
Please remember to format all code posts.

Sorry for not formatting it (it is not easily done while on mobile version).
Can you name one/two examples for external references to the objects ?
Thanks!
I mean if you have other variables referencing the objects you’re deleting then the objects won’t be garbage collected till those variables are cleared or fall out of scope.
I simply could not assume (in my answer) that you only put the objects in a display group. I didn’t know if you’d also used variables somewhere to refer to specific objects in that group.
Can you give one simple example?
Word variable is not so clear…
local group = display.newGroup() display.newCircle( group, 10, 10, 10 ) -- 1 local tmp = display.newCircle( group, 10, 10, 10 ) -- 2 display.newCircle( group, 10, 10, 10 ) -- 3 display.remove( group ) group = nil -- Circle 1 and 3 are removed and will be garbage collected. -- Circle 2 (referenced by variable 'tmp') is removed, but will not be -- garbage collected till 'tmp' is nilled or falls out of scope
Got you!
I almost always do:
local tmp = display.newCircle( group, 10, 10, 10 ) -- 2
It is a habit.
Last question.
Why nil is not required here (as you mentioned above).
I saw so many people doing nil here:
for i = 1, myGroup.numChildren do display.remove (myGroup[i]) end
Thanks Ed! 