Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

newTableView memory management.
Started by ammar71 Oct 28 2019 12:16 PM

1 reply to this topic
widget newtableview

Best Answer ammar71 , 20 November 2019 - 04:37 PM

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.

[TOPIC CONTROLS]
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

ammar71

[GLOBAL: userInfoPane.html]
ammar71
  • Enthusiast

  • 98 posts
  • Corona SDK

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.

 



[TOPIC: post.html]
#2

ammar71

[GLOBAL: userInfoPane.html]
ammar71
  • Enthusiast

  • 98 posts
  • Corona SDK

  Best Answer

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.




[topic_controls]
[/topic_controls]

Also tagged with one or more of these keywords: widget, newtableview