Should I nil out a variable if I'm going to use it again?

I’m doing a lot of image replacements where I just do a removeSelf on the image and then create a new image using the same variable name directly after I removeSelf. Is there any point to nil out the value of the object if I’m going to use it again?

Also side question does display.remove() nil out the object or does it just check if it exists and then remove it but you still have to nil it out for garbage collection?

Hi @dmglakewood,

There shouldn’t be a need to nil out the reference if you’re just going to “replace” it with another image. display.remove() will not automatically nil out the reference (it will only remove the display element), but as long as you don’t have it locked in memory (the reference) as a global or tied to some other non-disposable reference like a containing table, the Lua garbage collector should clean it up naturally.

Brent

Hi @dmglakewood,

There shouldn’t be a need to nil out the reference if you’re just going to “replace” it with another image. display.remove() will not automatically nil out the reference (it will only remove the display element), but as long as you don’t have it locked in memory (the reference) as a global or tied to some other non-disposable reference like a containing table, the Lua garbage collector should clean it up naturally.

Brent