Is this a Memory Leak on display groups or is it me?

I have isolated this piece of exampled code as the culprit that causes my App to keep gobbling up more and more memory until the App freezes and halts. As best I know, I have nilled out the display group and forced garbage collection and am looking for advice on this.

As you keep hitting the “OK” button you will see the memory use incrementing.

Bug or bad programming?

[code]
– MemLeak1 - a test of display group display taking up incremental system memory

local bkg = display.newRect( 0, 20, display.contentWidth, display.contentHeight )
bkg:setFillColor( 255, 255, 255 )

local box = display.newRect( 100, 50, 120, 310 )
box:setFillColor( 200, 200, 200 )

local textBox = display.newText( “”, 180, 410, native.systemFont, 16 )
textBox:setTextColor( 0, 0, 0 )

local listData = {} – populate wth 200 numbers
for idx = 1, 200 do
listData[idx] = idx
end

– generate a new listGroup object
local listGroup = display.newGroup()
local startIdx = -13

local goBtn = display.newGroup()
goBtn.x = 130
goBtn.y = 366

local boxBtn = display.newRect( 0, 0, 60, 40 )
boxBtn:setFillColor( 242, 236, 160 )
goBtn:insert( boxBtn )

local okBtn = display.newText( “OK”, 12, 2, native.systemFontBold, 24 )
okBtn:setTextColor( 0, 0, 0 )
goBtn:insert( okBtn )

local function renderListData()
– clear out any previous display group
listGroup:removeSelf()
listGroup = nil
listGroup = display.newGroup()

local lastY = 52 – property to track current position
local newCell, lbl0

– in this section, build a newCell display group that holds one line of data to display.
for currentIdx = startIdx, startIdx + 14 do
newCell = display.newGroup()
newCell.x = 110
newCell.y = lastY
lastY = lastY + 20

lbl0 = display.newText( listData[currentIdx], 2, 2, native.systemFont, 14 )
lbl0:setTextColor( 0, 0, 0 )
newCell:insert( lbl0 )
lbl0 = nil

listGroup:insert( newCell )
newCell = nil
end

end

local function boxBtnListener()
startIdx = startIdx + 14
if ( startIdx > 186 ) then startIdx = 1 end
renderListData()

collectgarbage(“collect”)
txt0 = "System Memory : " … collectgarbage(“count”) – gets the memory used by the LUA code
textBox.text = txt0
end
boxBtn:addEventListener(“tap”, boxBtnListener )

[/code] [import]uid: 6114 topic_id: 2742 reply_id: 302742[/import]

Well, you store a group (newCell) inside a group (listGroup).

I am not sure about it, but when you remove the listGroup, the items (newCell) are deleted but not the newText items inside each newCell.
Why do you store the newText objects inside the newCell item and store that in the listGroup?

Store the newText items inside the listGroup directly, or… before you remove the listGroup, loop through its children and remove them manually. [import]uid: 5712 topic_id: 2742 reply_id: 8219[/import]

Problem Solved!

@MikeHart - thanks for the brain jar.

I errantly thought that removing a group automatically removed its children, this was my big mistake. I need to hierarchically loop through all of the children of children to functionally wipe out the whole “family”. Looks like those un-removed children would stay around like some kind of Lua “ghosts”, if not directly removed.

In my live App, the newCell contains an image, a background rectangle and text, so needs to be its own group. I omitted the image and background rectangle for clarity in this example, which still illustrated the problem.

Using this revised code for cleanup is quite stable now for memory usage.

local function renderListData()  
-- clear out any previous display group  
 for idx = listGroup.numChildren, 1, -1 do  
 newCell = listGroup[idx]  
 for idx2 = newCell.numChildren, 1, -1 do  
 newCell:remove( idx2 )  
 end  
 listGroup:remove( idx )  
 end  
 listGroup:removeSelf()  
 listGroup = display.newGroup()  
--  

On to the next thing.

[import]uid: 6114 topic_id: 2742 reply_id: 8256[/import]

I think everything gets removed which is a direct child of its parent. But you had the Grandparent:Parent:Child relationship, where only the parent got removed.

Also be aware that display objects only get removed totally, as long you did n’t had added any custom properties to it. If you did, Corona removes any object related properties but leaves the rest inside a table. And that you have to NIL out so it will be collected later on. [import]uid: 5712 topic_id: 2742 reply_id: 8287[/import]

@MikeHart

Thanks for that housekeeping advice. Learning which of that is “Programmer Responsibility” is new ground in this environment. [import]uid: 6114 topic_id: 2742 reply_id: 8290[/import]