How to remove display objects declared with the same variable

So when i create two different lines using the same variable and try to use display.remove, only the recently created line is removed.

I am aware that the reference to the previous line is lost. But is there a way to remove that too.

And i am also aware that i can use a hash table to create two or more lines using same variable. But what i want to know is whether any method exists to remove the previous line.

Example:-

local line

line = display.newLine(10, 20, 50, 60) – First

line = display.newLine(50, 70, 60, 40) – Second

display.remove(line)

line = nil.

Here only the second line gets removed. But now i have lost reference to the first line. So i want to know if there is a way to remove the first line.

I’m just asking to know if its possible.

You’ll probably want to read this: http://lua-users.org/wiki/GarbageCollectionTutorial.

There are means of removing variables in Lua even after you’ve lost reference to them. In Corona, perhaps the easiest approach of removing display objects is to simply add both lines into a display group. Then you’ll retain a reference to those objects via the group, e.g.

 

local myGroup = display.newGroup() local line line = display.newLine(myGroup, 10, 20, 50, 60) -- group child #1 line = display.newLine(myGroup, 50, 70, 60, 40) -- group child #2 display.remove(line) line = nil display.remove(myGroup[1])

Still, this isn’t something that you should knowingly do. I would ask you a counter question: If you know all that, then why would you want to create a problem for yourself by creating two line(s) and intentionally losing the reference to the first line?

I’ll take a look into the article you have sent. And to answer your question, i sometimes make the above mistake when there is looping involved. I was curious to find out if there was a way to remove the objects without reference.i thought maybe i can call garbage collection process to free up the memory.

I apologize if you felt my question a bit dumb. Thanks again for pointing out to the article.   

It isn’t a dumb a question at all, but I think that this is a classic case of figuring out how to patch a problem instead of actually fixing the cause of the problem.

It is always better to fix the root of the problem instead of trying to mitigate the negative results.

You’ll probably want to read this: http://lua-users.org/wiki/GarbageCollectionTutorial.

There are means of removing variables in Lua even after you’ve lost reference to them. In Corona, perhaps the easiest approach of removing display objects is to simply add both lines into a display group. Then you’ll retain a reference to those objects via the group, e.g.

 

local myGroup = display.newGroup() local line line = display.newLine(myGroup, 10, 20, 50, 60) -- group child #1 line = display.newLine(myGroup, 50, 70, 60, 40) -- group child #2 display.remove(line) line = nil display.remove(myGroup[1])

Still, this isn’t something that you should knowingly do. I would ask you a counter question: If you know all that, then why would you want to create a problem for yourself by creating two line(s) and intentionally losing the reference to the first line?

I’ll take a look into the article you have sent. And to answer your question, i sometimes make the above mistake when there is looping involved. I was curious to find out if there was a way to remove the objects without reference.i thought maybe i can call garbage collection process to free up the memory.

I apologize if you felt my question a bit dumb. Thanks again for pointing out to the article.   

It isn’t a dumb a question at all, but I think that this is a classic case of figuring out how to patch a problem instead of actually fixing the cause of the problem.

It is always better to fix the root of the problem instead of trying to mitigate the negative results.