Setting object to nil in a function

Hello,

I created a convenience function to remove display objects:

function removeDisplayObject(obj)     display.remove(obj)     obj = nil end

However, when I call the function on a display object…

removeDisplayObject(a) if (a == nil) then print "object is nil" else print "object is not nil" end 

… the display object passed to the function isn’t being set to nil. Anyone know why? My goal is to properly remove a display object with a single code statement.

Thanks!

John

You have to nil out “a” outside of the function.

The variable “obj” is only local inside the function and gets niled out automatically.

So do the following:

function removeDisplayObject(obj)     display.remove(obj) end   removeDisplayObject(a) a = nil if (a == nil) then print "object is nil" else print "object is not nil" end

But as you can see, it’s a bit redundant to call function with only one function in it.

So you can just do this.

display.remove(a) a = nil if (a == nil) then print "object is nil" else print "object is not nil" end

In one line, you can do this (and I think removeSelf gracefully handles the object already being removed)

[lua]

    myObject = myObject:removeSelf()

[/lua]

You can go the pcall route if you want to check for myObject being nil, all in one line…

I think it should be noted that the line above will set ‘myObject’ to nil simply because ‘removeSelf’ has no return value, and so Lua defaults that value to ‘nil’.

http://docs.coronalabs.com/api/type/DisplayObject/removeSelf.html

That function removes the display object and converts it into a standard Lua table that should be garbage collected. But there is a caveat here, if anything is still referencing the aforementioned table, it will not be garbage collected (but instead be treated as a standard Lua table).

So it is better practise to always set the variable to nil, after removing it from the display.

You have to nil out “a” outside of the function.

The variable “obj” is only local inside the function and gets niled out automatically.

So do the following:

function removeDisplayObject(obj)     display.remove(obj) end   removeDisplayObject(a) a = nil if (a == nil) then print "object is nil" else print "object is not nil" end

But as you can see, it’s a bit redundant to call function with only one function in it.

So you can just do this.

display.remove(a) a = nil if (a == nil) then print "object is nil" else print "object is not nil" end

In one line, you can do this (and I think removeSelf gracefully handles the object already being removed)

[lua]

    myObject = myObject:removeSelf()

[/lua]

You can go the pcall route if you want to check for myObject being nil, all in one line…

I think it should be noted that the line above will set ‘myObject’ to nil simply because ‘removeSelf’ has no return value, and so Lua defaults that value to ‘nil’.

http://docs.coronalabs.com/api/type/DisplayObject/removeSelf.html

That function removes the display object and converts it into a standard Lua table that should be garbage collected. But there is a caveat here, if anything is still referencing the aforementioned table, it will not be garbage collected (but instead be treated as a standard Lua table).

So it is better practise to always set the variable to nil, after removing it from the display.

magine the following:

 

function triggerTimer(delay, callback)

   local handle = timer.performDelay(10000, callback, 0)

   saveHandle(handle)

end

 

… later  (after 5 seconds)…  (here the timer is valid!)

 

self.removeAllTimers()

  for each handle do   – ‘each’ is just to simplify …

    timer.cancel(handle)

    handle = nil   –  is this correct?

  end

end

 

 

My question is: when I set to nil the handle, i’m really destroying the handle to be GC?

I red somewhere that set to nil inside a function is not valid…

 

thanks

timer.cancel() allocates some memory, this is cleared when it’s canceled. However, timer.cancel() returns a table. That table takes up some memory. Nilling it will let GC clear it.  You may think it just overwrites the varaible, but Lua can actually trigger code when setting variables.

Or you can just say “It just works”

Rob

magine the following:

 

function triggerTimer(delay, callback)

   local handle = timer.performDelay(10000, callback, 0)

   saveHandle(handle)

end

 

… later  (after 5 seconds)…  (here the timer is valid!)

 

self.removeAllTimers()

  for each handle do   – ‘each’ is just to simplify …

    timer.cancel(handle)

    handle = nil   –  is this correct?

  end

end

 

 

My question is: when I set to nil the handle, i’m really destroying the handle to be GC?

I red somewhere that set to nil inside a function is not valid…

 

thanks

timer.cancel() allocates some memory, this is cleared when it’s canceled. However, timer.cancel() returns a table. That table takes up some memory. Nilling it will let GC clear it.  You may think it just overwrites the varaible, but Lua can actually trigger code when setting variables.

Or you can just say “It just works”

Rob