RemoveEventListener existential question ?

Hi,

There is something that is still unclear for me in term of RemoveEventListener. When we do the following code :

local group = display.newGroup()

 

local function onTapRect(event)

 

end

local rect = display.newRect( group, x, y, w, h)
rect:addEventListener ( “tap”, onTapRect)
rect.isHitTestable = true

rect = nil
 

group:insert(rect)

 

If later on, I call group:removeSelf() then group = nil, will the eventListener be removed from the rect when it is destroyed since it is part of the group ?

thanks

There are two things to answer here.  First, group:removeSelf() removes the children and deallocates their memory. 

Now the second part, the event listener:  The code is code.  It never gets removed.  When you do an :addEventListener() you’re just assigning the function to the object which is simply a memory address added to the table that represents the object.  In other words, it only adds about 4 bytes to the object.  When you nil out the object, that table gets emptied and is freed on the next garbage collection pass.  This in effect breaks the association of the function to the object. 

I know that Runtime listeners get added to an event table, but for listeners on objects, I don’t believe that’s the case, so simply removing the object severs that relationship.  But also consider this, for touch and tap handlers…  if the object is gone you don’t have anything to touch or interact with.

Rob

There are two things to answer here.  First, group:removeSelf() removes the children and deallocates their memory. 

Now the second part, the event listener:  The code is code.  It never gets removed.  When you do an :addEventListener() you’re just assigning the function to the object which is simply a memory address added to the table that represents the object.  In other words, it only adds about 4 bytes to the object.  When you nil out the object, that table gets emptied and is freed on the next garbage collection pass.  This in effect breaks the association of the function to the object. 

I know that Runtime listeners get added to an event table, but for listeners on objects, I don’t believe that’s the case, so simply removing the object severs that relationship.  But also consider this, for touch and tap handlers…  if the object is gone you don’t have anything to touch or interact with.

Rob