newTableView memory management.

Hi all,

I am experimenting with the widget.newTableView.

https://docs.coronalabs.com/api/library/widget/newTableView.html

I noticed that in the example, in the onRenderEvent we have:

local rowTitle = display.newText( row, "Row " .. row.index, 0, 0, nil, 14 )

Every time we scroll up or down, newText is created on the fly. There is no code to delete the ones that scroll off the view. Is that a good idea or could that lead to a memory leak?

Also, I am trying to highlight (change the colour) of a row when it is touched. For that I would need a reference to the newText object. I can keep references to those in a table as follows:

local rows = {}

Then in the RenderEvent:

rows[event.row.index] = display.newText( row, "Row " .. row.index, 0, 0, nil, 14

But again, every time a row is rendered, a newText object is created and the reference to the old one is lost.

Should I check the value of rows[event.row.index] before assigning the new newText and try to remove the old refernce with display.remove(rows[event.row.index]) or is that not needed?

Many thanks.

I finally solved this issue so I am documenting here what I was doing wrong (for my future self when I make this mistake again).

In the onRenderEvent I was declaring a local variable to refer to the newText

local rowTitle = display.newText( row, "Row " .. row.index, 0, 0, nil, 14 )

Then when I hide the scene I was doing the following:

display.remove(rowTitle) rowTitle = nil

Display.remove doesn’t complain when it doesn’t find rowTitle (which is a local variable inside onRenderEvent )

When I changed that to 

rowTitle.removeSelf() rowTitle = nil

I got an error. 

Declaring rowTitle at the scene level instead of inside onRenderEvent solved the problem.