Memory Leak in List Views (tableView.lua)

The listView:cleanUp() code in the tableView.lua file does not properly free up all of the resources in the list. It is still calling the old item:remove(i) code instead of recursively removing everything in the group.

First, I suggest that a new API call be added to properly recurse through a Group to remove all of the items in it. The group:removeSelf() call does NOT recurse to the children items. This is a huge problem with much of the user-submitted code I have been looking at. People just don’t really seem to understand memory management (even with the good articles available). I think Corona could be made more stable by addressing some of this in the core API.

In any case, I added the following function to tableView.lua (taken from the Director project)
[lua]local function removeGroup( curGroup, level )
level = level or 0
if curGroup.numChildren then
while curGroup.numChildren > 0 do
removeGroup( curGroup[curGroup.numChildren], level+1 )
end
if level > 0 then
curGroup:removeSelf()
end
else
curGroup:removeSelf()
curGroup = nil
return
end
end[/lua]
then I modified the listView:cleanUp function to look like this:
[lua]function listView:cleanUp()
Runtime:removeEventListener(“enterFrame”, moveCat )
Runtime:removeEventListener(“enterFrame”, scrollList )
Runtime:removeEventListener( “enterFrame”, showHighlight )
Runtime:removeEventListener(“enterFrame”, trackVelocity)
local i
for i = self.numChildren, 1, -1 do
self[i]:removeEventListener(“touch”, newListItemHandler)
end
removeGroup(self)
listView = nil
end[/lua]
This removed the memory leak that was occurring. I didn’t see a separate forum for List Views, so hopefully this is the correct place to report this. [import]uid: 12529 topic_id: 6675 reply_id: 306675[/import]

Btw, the line
[lua]local i[/lua]
is not needed since Lua makes all “for” loops use local variables.

Also note the proper changes to reference the “self” variable instead of the hard-coded “listView” references that were in the original code. [import]uid: 12529 topic_id: 6675 reply_id: 23277[/import]

Hi MPotter,

As a new user, I really appreciate the assistance you are providing in these Forums.

I am learning alot here but I agree that memory management is the thing I least understand. Coming from a Flash background, I haven’t really dealt with this issue.

Plus I can’t really figure out how to detect a ‘memory leak’. In the Simulator? The Debugger?

It would be nice to see more info on this topic. [import]uid: 22392 topic_id: 6675 reply_id: 23487[/import]

Greetings,

this is great stuff… What I want to know is how do I call this cleanUp function. Since listView is local and so is currentTarget.

I added

function cleanUp()
currentTarget:cleanUp();
end

to tableView.lua but I am sure there is a smarter way ?

T. [import]uid: 12684 topic_id: 6675 reply_id: 53625[/import]

Very interesting function OP, I think I need to stare at it for a week for possible implications!

I’m sure you’ve probably figured out your own answer by now konijn, but because newList{} returns listView, you can call the cleanUp() outside of the function.

myList = tableView.newList{ stuff } myList:cleanUp()

I’m not sure what [lua]currentTarget[/lua] is, but usually you call cleanUp() from inside of an event listener, so I suppose you could try [lua]event.target:cleanUp()[/lua]?

[import]uid: 41884 topic_id: 6675 reply_id: 56291[/import]