Does group:removeSelf() affect children?

I was wondering how far I need to go to remove display objects and variables.

Lets say you have a group that you’ve inserted multiple objects into such as rect, line, text.

local grp = display.newGroup() grp.foo = "abc" gtp.bar = 123 local rect = display.newRect(0,0,100,100) grp:insert(rect) local line = display.newLine(0,0,100,100) grp:insert(line) --... at some point later, remove the group grp:removeSelf() grp = nil

What happens to the variables, rect and line? Does it get lost until it’s collected by the gc? Does calling removeSelf on the group also recurse through the children? What about the variables foo and bar?

Extra question: Does the above answer apply to other types such as display.newContainer and composer.newScene?

Since this makes all my screen objects (which happen to all be individual groups) disappear:

 local Stage = display.getCurrentStage() local Count = Stage.numChildren for i = 1,Count do Stage[1]:removeSelf() end

I feel confident removeSelf() is recursive for nested groups/containers as claimed in the docs.

Based on the docs and behavior, I assume rect and line retain references to the “Lua side” of display objects whose “C side” (or maybe “C++ side”, not having source to the engine) has been deleted. As long as they are in scope, of course. You can either constantly worry about setting such variables to nil, or just do the perhaps more natural thing and create such objects in modular functions so that variables of that ilk naturally go out of scope and drop their references long before the associated objects might be garbage collected.

Thanks for the reply. I didn’t find that doc when I had posted earlier so that was helpful.

I did eventually get around to testing this though, however I am not sure if my method is scientific in any way.

This was my test code, where I force gc to collect every frame and print out the used lua and tex mem.

After 5 seconds, I call removeSelf on the group.

collectgarbage() local start = collectgarbage("count") local grp = display.newGroup() grp.rect = display.newRect(0,0,100,100) grp.line = display.newLine(0,0,100,100) grp:insert(grp.rect) grp:insert(grp.line) Runtime:addEventListener("enterFrame", function() collectgarbage() print(collectgarbage("count")-start, system.getInfo("textureMemoryUsed")) end) timer.performWithDelay(5000, function() grp:removeSelf() grp = nil end)

Note: The commented out code, I found, returned a different amount of memory compared to directly inserting a call to display.newObject into the group.

The difference was maybe 50-100 bytes at the most so it’s not too concerning in this test, however what if I have hundreds of these objects being created and thrown away? I know that method is bad practice but it does make my code a lot more modular and easy to write.

I have a feeling I’m just wasting time on this and I should just let corona and lua deal with it.

Removing a display group removes it’s children.

Rob

Since this makes all my screen objects (which happen to all be individual groups) disappear:

 local Stage = display.getCurrentStage() local Count = Stage.numChildren for i = 1,Count do Stage[1]:removeSelf() end

I feel confident removeSelf() is recursive for nested groups/containers as claimed in the docs.

Based on the docs and behavior, I assume rect and line retain references to the “Lua side” of display objects whose “C side” (or maybe “C++ side”, not having source to the engine) has been deleted. As long as they are in scope, of course. You can either constantly worry about setting such variables to nil, or just do the perhaps more natural thing and create such objects in modular functions so that variables of that ilk naturally go out of scope and drop their references long before the associated objects might be garbage collected.

Thanks for the reply. I didn’t find that doc when I had posted earlier so that was helpful.

I did eventually get around to testing this though, however I am not sure if my method is scientific in any way.

This was my test code, where I force gc to collect every frame and print out the used lua and tex mem.

After 5 seconds, I call removeSelf on the group.

collectgarbage() local start = collectgarbage("count") local grp = display.newGroup() grp.rect = display.newRect(0,0,100,100) grp.line = display.newLine(0,0,100,100) grp:insert(grp.rect) grp:insert(grp.line) Runtime:addEventListener("enterFrame", function() collectgarbage() print(collectgarbage("count")-start, system.getInfo("textureMemoryUsed")) end) timer.performWithDelay(5000, function() grp:removeSelf() grp = nil end)

Note: The commented out code, I found, returned a different amount of memory compared to directly inserting a call to display.newObject into the group.

The difference was maybe 50-100 bytes at the most so it’s not too concerning in this test, however what if I have hundreds of these objects being created and thrown away? I know that method is bad practice but it does make my code a lot more modular and easy to write.

I have a feeling I’m just wasting time on this and I should just let corona and lua deal with it.

Removing a display group removes it’s children.

Rob