removing objects from group or a table with obj:touch or obj.touch

I know you will probably say this has been covered multiple times, but still I am not quite sure what is the best approach to do this. I will try my best to explain my dilemma that might help other folks as well.

I’ve been trying different methods to clean a scene. I have read numerous answeres, but I am still no clear about this. This is my exeperiment using a slightly modified example from this forum:(note - I have previously forward declared parentGroup1 and rect1)

  local function testFun(self, event)    self.alpha = 0   end   parentGroup1 = display.newGroup()  local childGroup1 = display.newGroup( parentGroup1:insert( childGroup1 )   local childGroup2 = display.newGroup(); childGroup1:insert( childGroup2 )   childGroup1.name = "childGroup1"   childGroup2.name = "childGroup2"   rect1 = display.newRect( parentGroup1, 100, 100, 100, 100 )   rect1.name = "rect1"   local rect2 = display.newRect( childGroup1, 200, 200, 100, 100 )   local rect3 = display.newRect( childGroup2, 300, 300, 100, 100 )   rect1.touch = testFun   rect1:addEventListener("touch", rect1)

on scene:hide I use this function to see what happens:

     for i = parentGroup1.numChildren, 1, - 1 do        ​local child = parentGroup1[i]         print(child.name)         if child.touch then           child:removeEventListener("touch", child)         end         if child then           child:removeSelf()           child = nil         end       end       parentGroup1:removeSelf()       parentGroup1 = nil

on scene:destroy I use the following to check if the cleanup is ok on rect1

if nil ~= rect1 then print("rect 1 still Here") end

and sure enough I get the “rect 1 still Here” when I leave the scene. I don’t think it should be there and I don’t get this if I just remove it like this:

      rect1:removeSelf()       rect1 = nil

but this way I would have remove each object one by one.

So my questions are:

 - can I remove objects with other references just by removing and niling the table or a group? 

 - can I remove objects with iterating over a group or a table and if I can how, since on this example some references are obviously still there?

  • is it enough to just do object:removeSelf() and object = nil, on an object with object:touch or object.touch eventListener on them or do I have to remove eventListener also.

To answer your questions…

  1. To completely remove an object all references to it must be nilled first.

  2. Yes, you can unless rule 1 is broken.

  3. No, you don’t need to explicitly remove touch handlers as Corona will do this automatically for you.

great, thank you for your answers. Regarding rule 1 though - do I have to completely nill all references or is removeSelf enough? And like this experiment shows then object:removeSelf( ) is not the same as removing it with group iteration child:removeSelf(), as the first returns nil and the second one doesn’t.

I have one more question regarding touch function - please see example on top testFun…Would it be ok, if I could make a function on object like this:

function object.touch ( )  obj.alpha = 0 end

or

function object:touch ( )  self.alpha = 0 end

or is it a better practice to first create a function separatly like the example above?

To answer your questions…

  1. To completely remove an object all references to it must be nilled first.

  2. Yes, you can unless rule 1 is broken.

  3. No, you don’t need to explicitly remove touch handlers as Corona will do this automatically for you.

great, thank you for your answers. Regarding rule 1 though - do I have to completely nill all references or is removeSelf enough? And like this experiment shows then object:removeSelf( ) is not the same as removing it with group iteration child:removeSelf(), as the first returns nil and the second one doesn’t.

I have one more question regarding touch function - please see example on top testFun…Would it be ok, if I could make a function on object like this:

function object.touch ( )  obj.alpha = 0 end

or

function object:touch ( )  self.alpha = 0 end

or is it a better practice to first create a function separatly like the example above?