correctly removing objects, their listeners and transitions etc.

Hi

Would someone please confirm for me that my code is clean, i.e. that it’s cleaning up after itself?

I created a display group, then a text object. I added the text object to a display group, added an event listener and then applied a transition to it. Then later I removed the group, hopefully removing everything in one sweep.

myGroup=display.newGroup()      local myText = display.newText(myGroup,"welcome" , 100,100, FONT\_NAME, 80 ) myText:addEventListener("touch",myListener) myText.transitionfrom = transition.from( myText, { time=1000, delay=0, alpha=0}) myText.transitionto = transition.to( myText, { time=1000, delay=4000, alpha=0} ) ... myGroup:removeSelf()  

thanks,

David

Well, that depends.

On the face of it, yes it does. In terms of what you see, it will remove the eventListener. 

The fly in the ointment is, or could be, the myText local. It is usually a good idea to nil this one you’ve used it.

The reason is lua closures. If your code carried on to declare a function in this scope, something like:

local someReason = function() doStuffSomewhereElse() end

this will create a closure. Closures have access to local scope. So if you were to have this immediately after the transitionTo line this closure would contain a reference to your myText - when you called the doStuffSomewhereElse() function, it would ‘know’ about myText. So therefore it could not garbage collect it (free the memory) because it could still be needed.

If you preceded this with myText = nil it would still ‘know’ about myText in the closure, but it wouldn’t contain a reference to the displayObject so it wouldn’t matter.

thanks.

Well, that depends.

On the face of it, yes it does. In terms of what you see, it will remove the eventListener. 

The fly in the ointment is, or could be, the myText local. It is usually a good idea to nil this one you’ve used it.

The reason is lua closures. If your code carried on to declare a function in this scope, something like:

local someReason = function() doStuffSomewhereElse() end

this will create a closure. Closures have access to local scope. So if you were to have this immediately after the transitionTo line this closure would contain a reference to your myText - when you called the doStuffSomewhereElse() function, it would ‘know’ about myText. So therefore it could not garbage collect it (free the memory) because it could still be needed.

If you preceded this with myText = nil it would still ‘know’ about myText in the closure, but it wouldn’t contain a reference to the displayObject so it wouldn’t matter.

thanks.