What happens when a local DisplayObject goes out of scope?

I’m trying to understand what happens to local variables when they go out of scope after having been displayed.  Consider this Lua code:

[lua]

local function localAddCircle()

    local circle = display.newCircle(200,200,10)

    print (type(circle)) – prints “table”

end

localAddCircle()

print (type(circle)) – prints nil

print (#display) – prints 0

[/lua]

Since circle is a local variable, it’s not accessible outside of the function.  I assume it’s garbage collected.  I understand this part.

I don’t understand the relationship between the local circle variable and the display table.  As you can see, the length of the display table is 0.  It’s empty, yet the circle remains displayed on the screen.

What’s going on under the covers here?

Yes, you are right, it’s not accessable outside function, but 2 wrongs heres:

  1. After you leave localAddCircle() function, circle still will be on the screen - it won’t be garbage collected because it’s display object and display objects are stored in “render queue/storage” underneath in Corona engine - the only option to remove them is to call :removeSelf() + nil-ing variable holding image.

  2. You cannot write #display wanting in return number of objects! Corona SDK is graphics engine written (probably) in C language and you do not have access to it. To interact with engine, Corona gives you so called interface, meaning giving you Lua function calls which calls underneath C code.

It’s like microwave oven buttons :stuck_out_tongue: Lua is such buttons, with which you can program your microwave oven (Corona Engine), but it won’t say you what’s inside.

Thanks.  I guess there’s some black magic happening under the hood.  I’ll look for documentation or articles about the render/queue storage.

You probably won’t find it. Just keep in mind that you do not have access to all created objects until you assign them to some variable. Only if you put them into displayGroups then you can access .childNum to get number of objects contained in group.

Yes, you are right, it’s not accessable outside function, but 2 wrongs heres:

  1. After you leave localAddCircle() function, circle still will be on the screen - it won’t be garbage collected because it’s display object and display objects are stored in “render queue/storage” underneath in Corona engine - the only option to remove them is to call :removeSelf() + nil-ing variable holding image.

  2. You cannot write #display wanting in return number of objects! Corona SDK is graphics engine written (probably) in C language and you do not have access to it. To interact with engine, Corona gives you so called interface, meaning giving you Lua function calls which calls underneath C code.

It’s like microwave oven buttons :stuck_out_tongue: Lua is such buttons, with which you can program your microwave oven (Corona Engine), but it won’t say you what’s inside.

Thanks.  I guess there’s some black magic happening under the hood.  I’ll look for documentation or articles about the render/queue storage.

You probably won’t find it. Just keep in mind that you do not have access to all created objects until you assign them to some variable. Only if you put them into displayGroups then you can access .childNum to get number of objects contained in group.