Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
"MoKaLux",can you help me ? I ran into some problems! — Gideros Forum

"MoKaLux",can you help me ? I ran into some problems!

luyimoonluyimoon Member
edited December 2021 in General questions
1.a sprite,has no parent, is any function(method) can keep it not destroyed?

2.a sprite,it parent is stage ,when "stage:removeChild(sprite)" , is any function(method) can destroy it frome memory?

3.a sprites has no parent,how long destroy from memory? and what is the process?

Comments

  • MoKaLuxMoKaLux Member
    edited December 2021 Accepted Answer
    First I would like to say something: "only worry about performance and optimisation when you absolutely need to, Gideros does a lot of the work behind the scene".

    Now I will try to answer your questions :)
    1- when your sprite has no parent (not added to stage) then it is not displayed on the screen but is kept in memory
    2- when you do stage:removeChild(sprite) lua and its garbage collector will at some point automatically remove it from memory, to be sure you can set sprite = nil
    3- same as above

    Note if you want to hide (not destroy) your sprite you can call sprite:setVisible(false), ...

    Destroy from memory is called garbageCollector. From the wiki:
    https://wiki.gideros.rocks/index.php/Introduction_to_Lua#Assignments_and_variables


    There are quite a few posts on the forum like these ones:
    https://forum.gideros.rocks/discussion/comment/61504/#Comment_61504
    https://forum.gideros.rocks/discussion/8019/doubts-about-texture-bitmaps-and-memory-consumption
    https://forum.gideros.rocks/discussion/7845/best-way-to-remove-a-sprite-that-contains-other-sprites-and-bitmaps
    https://forum.gideros.rocks/discussion/comment/56787/#Comment_56787
    ...

    giderians, please correct me if I am wrong ;)
    my growING GIDEROS github repositories: https://github.com/mokalux?tab=repositories
  • MoKaLux said:

    First I would like to say something: "only worry about performance and optimisation when you absolutely need to, Gideros does a lot of the work behind the scene".

    Now I will try to answer your questions :)


    giderians, please correct me if I am wrong ;)


    emm, Where is the problem?


    local container = Sprite.new()
    local ball1 = Bitmap.new(Texture.new("ball.png", true))
    ball1:setAnchorPoint(0.5, 0.5)
    ball1:setX(-50)
    container:addChild(ball1)

    local ball2 = Bitmap.new(Texture.new("ball.png", true))
    ball2:setAnchorPoint(0.5,0.5)
    ball2:setX(50)
    container:setPosition(150, 150)

    stage:addChild(container) -- add

    stage:removeChild(container) -- remove
    collectgarbage('collect')

    local timer = Timer.new(20000, 1)
    timer:addEventListener(Event.TIMER,
    function()
    print("111")
    stage:addChild(container) --- why still can addchild?
    end)
    timer:start()


  • MoKaLuxMoKaLux Member
    edited December 2021 Accepted Answer
    "why still can addchild?"
    so I guess you want to destroy container from memory?
    you need to set it to nil before collectgarbage.
    local container = Sprite.new()
     
    local ball1 = Bitmap.new(Texture.new("gfx/balls/mine.png", true))
    ball1:setAnchorPoint(0.5, 0.5)
    ball1:setX(-50)
     
    local ball2 = Bitmap.new(Texture.new("gfx/balls/mine2.png", true))
    ball2:setAnchorPoint(0.5,0.5)
    ball2:setX(50)
     
    -- position
    container:setPosition(150, 150)
    -- order
    container:addChild(ball1)
    stage:addChild(container) -- add
    stage:removeChild(container) -- remove
    container = nil
    collectgarbage('collect')
     
    local timer = Timer.new(2000, 1)
    timer:addEventListener(Event.TIMER, function()
    	print("111")
    	stage:addChild(container) --- error: container is now destroyed and cannot be used
    end)
    timer:start()
    my growING GIDEROS github repositories: https://github.com/mokalux?tab=repositories
  • luyimoonluyimoon Member
    edited December 2021
    MoKaLux said:

    "why still can addchild?"
    so I guess you want to destroy container from memory?---yes
    you need to set it to nil before collectgarbage.

    local container = Sprite.new()
     
    local ball1 = Bitmap.new(Texture.new("gfx/balls/mine.png", true))
    ball1:setAnchorPoint(0.5, 0.5)
    ball1:setX(-50)
     
    local ball2 = Bitmap.new(Texture.new("gfx/balls/mine2.png", true))
    ball2:setAnchorPoint(0.5,0.5)
    ball2:setX(50)
     
    -- position
    container:setPosition(150, 150)
    -- order
    container:addChild(ball1)
    stage:addChild(container) -- add
    stage:removeChild(container) -- remove
    container = nil
    collectgarbage('collect')
     
    local timer = Timer.new(2000, 1)
    timer:addEventListener(Event.TIMER, function()
    	print("111")
    	stage:addChild(container) --- error: container is now destroyed and cannot be used
    end)
    timer:start()

    so I guess you want to destroy container from memory? --- yes you are right

    1.if not set it to "nil",it will never be destroyed? even though it is local variable?

    2.if "container" has not parent and not set "nil", even though auto-collect garbage or manual 'collectgarbage()' ,it still exist?
  • MoKaLuxMoKaLux Member
    Accepted Answer
    1- yes, collectgarbage will only delete from memory objects which are nil
    2- yes it still exists because "gideros" thinks that you will use this object later in your game/app so it doesn't destroy it for you.
    my growING GIDEROS github repositories: https://github.com/mokalux?tab=repositories
  • luyimoonluyimoon Member
    edited December 2021
    MoKaLux said:

    "why still can addchild?"
    so I guess you want to destroy container from memory?
    you need to set it to nil before collectgarbage.

    local container = Sprite.new()
     
    local ball1 = Bitmap.new(Texture.new("gfx/balls/mine.png", true))
    ball1:setAnchorPoint(0.5, 0.5)
    ball1:setX(-50)
     
    local ball2 = Bitmap.new(Texture.new("gfx/balls/mine2.png", true))
    ball2:setAnchorPoint(0.5,0.5)
    ball2:setX(50)
     
    -- position
    container:setPosition(150, 150)
    -- order
    container:addChild(ball1)
    stage:addChild(container) -- add
    stage:removeChild(container) -- remove
    container = nil
    collectgarbage('collect')
     
    local timer = Timer.new(2000, 1)
    timer:addEventListener(Event.TIMER, function()
    	print("111")
    	stage:addChild(container) --- error: container is now destroyed and cannot be used
    end)
    timer:start()
    oh i see.





    Likes: MoKaLux

    +1 -1 (+1 / -0 )Share on Facebook
  • hgy29hgy29 Maintainer
    Accepted Answer
    In lua, as in javascript or ObjectiveC, the engine automatically detects if an object is alive (referenced) or dead (unreachable). To do this, it recursively scans all object tables starting from globals and thread stacks, marking objects it sees in the process. Then it scans a list of all objects that were ever allocated, and frees those that weren't reached by the previous step.
    So, to be freeable, an object must not be reachable through any reference, either those you set yourself in lua (a global, a local, an upvalue, a parameter of a still running function, etc), or those Gideros keeps under the hood (Sprites attached to the stage, a running MovieClip, etc).
    +1 -1 (+2 / -0 )Share on Facebook
  • hgy29 said:

    In lua, as in javascript or ObjectiveC, the engine automatically detects if an object is alive (referenced) or dead (unreachable). To do this, it recursively scans all object tables starting from globals and thread stacks, marking objects it sees in the process. Then it scans a list of all objects that were ever allocated, and frees those that weren't reached by the previous step.
    So, to be freeable, an object must not be reachable through any reference, either those you set yourself in lua (a global, a local, an upvalue, a parameter of a still running function, etc), or those Gideros keeps under the hood (Sprites attached to the stage, a running MovieClip, etc).

    thanks

    Likes: MoKaLux

    +1 -1 (+1 / -0 )Share on Facebook
Sign In or Register to comment.