Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Setting object to nil in a function
Started by nesguru Nov 03 2013 06:27 PM

- - - - -
6 replies to this topic
nil display object lua scope
[TOPIC CONTROLS]
This topic has been archived. This means that you cannot reply to this topic.
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

nesguru

[GLOBAL: userInfoPane.html]
nesguru
  • Observer

  • 22 posts
  • Corona SDK

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

 



[TOPIC: post.html]
#2

torbenratzlaff

[GLOBAL: userInfoPane.html]
torbenratzlaff
  • Contributor

  • 404 posts
  • Corona SDK

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


[TOPIC: post.html]
#3

mpappas

[GLOBAL: userInfoPane.html]
mpappas
  • Contributor

  • 527 posts
  • Enterprise

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

 

    myObject = myObject:removeSelf()

 

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



[TOPIC: post.html]
#4

horacebury

[GLOBAL: userInfoPane.html]
horacebury
  • Corona Geek

  • 3,070 posts
  • Corona SDK

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



[TOPIC: post.html]
#5

Danny

[GLOBAL: userInfoPane.html]
Danny
  • Corona Geek

  • 2,597 posts
  • Corona Staff

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.

[TOPIC: post.html]
#6

luisxpto32

[GLOBAL: userInfoPane.html]
luisxpto32
  • Observer

  • 16 posts
  • Corona SDK

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



[TOPIC: post.html]
#7

Rob Miracle

[GLOBAL: userInfoPane.html]
Rob Miracle
  • Moderator

  • 26,544 posts
  • Enterprise

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




[topic_controls]
[/topic_controls]