Cleaning Up Nested Groups

Hello. This might be a silly question, but I just want to make sure I’m doing it right to avoid having any memory leaks. Let’s say I have a table nested inside another table. Would the following method work to clean up both tables?

local group = display.newGroup() local textGroup = display.newGroup() textGroup.text = {} for i=1, 10 do textGroup.text[i] = display.newText('Text', 0, i\*20, native.systemFont) textGroup:insert(textGroup.text[i]) end group.text = display.newText('Display Group', 0, 0, native.systemFont) group:insert(group.text) group:insert(textGroup) for i=1, group.numChildren do group:remove(1) end

I know the code is a little messy, but it’s just for example purposes.

As you can see, one group has another group inside of it. This nested textGroup has several display objects. When I remove textGroup, am I also removing all display objects?

Thanks.

Hi @jjsanchezramirez,

When dealing with display objects in nested groups, you can simply remove the “parent” group and all display objects within child groups (and the parent group itself) will be removed.

Only if you somehow associated these object references to tables or variables elsewhere would they remain in memory and require further cleanup to prevent memory leaks.

Hope this helps,

Brent

Thank you, Brent. That does answer my question.

Hi @jjsanchezramirez,

When dealing with display objects in nested groups, you can simply remove the “parent” group and all display objects within child groups (and the parent group itself) will be removed.

Only if you somehow associated these object references to tables or variables elsewhere would they remain in memory and require further cleanup to prevent memory leaks.

Hope this helps,

Brent

Thank you, Brent. That does answer my question.