Old display.remove code in main.lua question...

I have this code in my main.lua and want to check if I can still use it without problems because it is based on a very old Corona tutorial:

local oldRemove = display.remove display.remove = function( o )         if o ~= nil then                                 Runtime:removeEventListener( "enterFrame", o )                 oldRemove( o )                 o = nil                         end end

Is it still okay to use when using the display.remove inside the rest of the code?

whats the point of using this old code?

event listeners are removed automatically when object is removed and nil’ed out.

display.remove(object)

object=nil

its all you need (plus the great and always updated online corona documentation)

I have no idea what’s the point… just found it! :slight_smile:

If it is safe to remove it I will.

forgive me, but that’s hilarious!  in general, you should NOT just go around pasting in random bits of code found on the internet, ESPECIALLY if you have no idea what they do!

however, it DOES in theory do something POTENTIALLY useful - removes Runtime  enterframe listeners from an object.  Runtime listeners are NOT otherwise automatically removed when a display object is removed from the display.  So if you DO that sort of thing (add runtime enterframe listeners to display objects) and don’t otherwise clean them up yourself, then maybe you DO need this code (to clean up things that you’re not cleaning up yourself)  but it’s just a “monkey patch” to make display.remove do the runtime cleanup also, there’s nothing “magic” going on that you couldn’t do yourself without it.

here’s the test case:

-- try this WITHOUT that found code, then try it WITH local rect = display.newRect(10,10,20,20) rect.enterFrame = function() print("hello") end Runtime:addEventListener("enterFrame", rect) display.remove(rect) -- Q: so why does console keep printing "hello"?!

I don’t know that Runtime enterFrame listeners get removed automatically. The preferred method to do this would be to use a finalize event, which is an event that gets called when a request to remove an object occurs. The finalize event could remove the enterFrame listener for a specific object. This is better than trying to remove it on every display object when they don’t all have enterFrame listeners.

http://docs.coronalabs.com/api/event/finalize/index.html

Rob

:wink: I guess this once was helpful so I always used a basic main.lua where this already was in.

Thanks for the details!

BTW: Is there a way to use this function to remove other objects added to objects, like this for example:

local rect = display.newRect(10,10,20,20) rect.myOtherRect = display.newRect(20,20,30,30) rect.myCircle = display.newCircle(0,0,20)

so with using:

display.remove (rect) rect=nil

will also remove the “subobjects” ?

And is there maybe a way to use a function to remove images in a table which also can contain all other kind of data?

Something like:

mytable.number=123 mytable.mystring="hello" mytable.myCircle=display.newCircle (0,0,30,30)

Is there a way to clear this table INCLUDING the correct removal for the circle object to free the memory for it correctly?

there are techniques you could use to “inspect and query” a table (potentially recursively) looking for contained items that “resemble” a display object then remove them too.

however, that is almost always the wrong approach.  it is not good practice to waste time creating “helper” functions that have to “guess” at what they’re supposed to do, just on the “theory” that they’ll save you some effort by not having to do it yourself correctly/properly longhand.  eventually you’ll find a case where your helper guesses wrong, and you’ll spend way more time hunting down that bug than if you had just done it correctly to begin with.

rather, be specific instead of general - everything that you create should know how to properly destroy itself.  that approach is way more reliable, and will also help isolate any memory bugs if they DO occur (just look in the code that destroys THAT thing)

unless you could know that your “helper” is 100% infallible then it will NOT actually be “helpful” in the long run.  (and it is essentially impossible to write infallible code if no constraints are present.  so, challenge:  do your best to write this proposed helper routine of yours, and i’ll bet i could find a failure case in a few seconds)

Guess you are right. Thanks!

One more thing: How can I look if a table entry is an image? Is there a way to “check” for it?

sort of, as i alluded to before, but there is no way to do so that can’t be tricked (into either false-positive, or false-negative), as also alluded to before.

Thanks for clarifying this!

also, aside, fwiw (just as a lesson, back to your original Q about should you keep that found code)

see that line “o = nil” in your original post?

that line does _ absolutely nothing _ - “o” is a local and will cease to exist all by itself once the function exits.

the presence of any such nonsense/do-nothing code should be a HUGE red flag before adopting any found code.

(i have my suspicions about what the original author might have THOUGHT that code did, but they’d be very very wrong if so)

dang! i was too quick and didnt catch that the event in question was a runtime event.

naturally it doesnt vanish by it self since you dont delete the “runtime” object the listener is attached to.

for other types of listeners like “touch”, and “tap” that are are attached to display objects, they should get removed with the objects.

my bad  -_-

whats the point of using this old code?

event listeners are removed automatically when object is removed and nil’ed out.

display.remove(object)

object=nil

its all you need (plus the great and always updated online corona documentation)

I have no idea what’s the point… just found it! :slight_smile:

If it is safe to remove it I will.

forgive me, but that’s hilarious!  in general, you should NOT just go around pasting in random bits of code found on the internet, ESPECIALLY if you have no idea what they do!

however, it DOES in theory do something POTENTIALLY useful - removes Runtime  enterframe listeners from an object.  Runtime listeners are NOT otherwise automatically removed when a display object is removed from the display.  So if you DO that sort of thing (add runtime enterframe listeners to display objects) and don’t otherwise clean them up yourself, then maybe you DO need this code (to clean up things that you’re not cleaning up yourself)  but it’s just a “monkey patch” to make display.remove do the runtime cleanup also, there’s nothing “magic” going on that you couldn’t do yourself without it.

here’s the test case:

-- try this WITHOUT that found code, then try it WITH local rect = display.newRect(10,10,20,20) rect.enterFrame = function() print("hello") end Runtime:addEventListener("enterFrame", rect) display.remove(rect) -- Q: so why does console keep printing "hello"?!

I don’t know that Runtime enterFrame listeners get removed automatically. The preferred method to do this would be to use a finalize event, which is an event that gets called when a request to remove an object occurs. The finalize event could remove the enterFrame listener for a specific object. This is better than trying to remove it on every display object when they don’t all have enterFrame listeners.

http://docs.coronalabs.com/api/event/finalize/index.html

Rob

:wink: I guess this once was helpful so I always used a basic main.lua where this already was in.

Thanks for the details!

BTW: Is there a way to use this function to remove other objects added to objects, like this for example:

local rect = display.newRect(10,10,20,20) rect.myOtherRect = display.newRect(20,20,30,30) rect.myCircle = display.newCircle(0,0,20)

so with using:

display.remove (rect) rect=nil

will also remove the “subobjects” ?

And is there maybe a way to use a function to remove images in a table which also can contain all other kind of data?

Something like:

mytable.number=123 mytable.mystring="hello" mytable.myCircle=display.newCircle (0,0,30,30)

Is there a way to clear this table INCLUDING the correct removal for the circle object to free the memory for it correctly?

there are techniques you could use to “inspect and query” a table (potentially recursively) looking for contained items that “resemble” a display object then remove them too.

however, that is almost always the wrong approach.  it is not good practice to waste time creating “helper” functions that have to “guess” at what they’re supposed to do, just on the “theory” that they’ll save you some effort by not having to do it yourself correctly/properly longhand.  eventually you’ll find a case where your helper guesses wrong, and you’ll spend way more time hunting down that bug than if you had just done it correctly to begin with.

rather, be specific instead of general - everything that you create should know how to properly destroy itself.  that approach is way more reliable, and will also help isolate any memory bugs if they DO occur (just look in the code that destroys THAT thing)

unless you could know that your “helper” is 100% infallible then it will NOT actually be “helpful” in the long run.  (and it is essentially impossible to write infallible code if no constraints are present.  so, challenge:  do your best to write this proposed helper routine of yours, and i’ll bet i could find a failure case in a few seconds)

Guess you are right. Thanks!

One more thing: How can I look if a table entry is an image? Is there a way to “check” for it?

sort of, as i alluded to before, but there is no way to do so that can’t be tricked (into either false-positive, or false-negative), as also alluded to before.