Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
Are timers deleted after calling Timer.stopAll()? — Gideros Forum

Are timers deleted after calling Timer.stopAll()?

MagnusviriMagnusviri Member
edited November 2012 in General questions
I have an app that I need to stop timers for a transition and so I'm using Timer.stopAll(). But I'm wondering if a timer hasn't been called, will it be garbage collected eventually? I'm not keeping any references to the timers, just the addEventListener, like this.
function afterDelayPerformFunction(delay, functionToPerform)
	local timer = Timer.new(delay, 1)
	timer:addEventListener(Event.TIMER, functionToPerform)
	timer:start()
end
 
function transition()
	Timer.stopAll()
end
Tagged:

Comments

  • i think so no (why ?)

    here's the my test result
    local myTimer = Timer.new(10,10000)
     
    myTimer:start()
     
    function onEnterFrame()
    	i=i+1
    	print(i,myTimer)
    	if i==10 then
    		Timer.stopAll()
    		--myTimer= nil
    	end
    end
    stage:addEventListener(Event.ENTER_FRAME,onEnterFrame,stage)
  • atilimatilim Maintainer
    edited November 2012 Accepted Answer
    Yes they're collected. (Running timers are never collected and not running timers are eligible to GC.)
    local myTimer = Timer.new(1000)
    myTimer:start()
     
    myTimer.proxy = newproxy(true)
    getmetatable(myTimer.proxy).__gc = function() print("collected!") end
     
    local i = 0
    myTimer:addEventListener(Event.TIMER, function()
    	print("tick.")
    	i = i + 1
    	if i == 3 then
    		Timer.stopAll()
    	end
    end)
     
    stage:addEventListener(Event.ENTER_FRAME, function() collectgarbage() end)
    There are only two ways to understand if an object is collected or not:
    1. Using the unofficial function newproxy
    2. Using weak tables

    @hgvyas123, in your example, you're accessing myTimer as an upvalue. Therefore, it's still accessible and not eligible to GC.

  • @hgvyas123, in your example, you're accessing myTimer as an upvalue. Therefore, it's still accessible and not eligible to GC.
    got that i need some reading on this topics didnt know about upvalue and newproxy function

    :)
  • Thanks!

    I'm guessing that a paused timer is not eligible for GC (this isn't something I'm concerned with now but I just wanted to make the thread complete).
  • atilimatilim Maintainer
    I'm guessing that a paused timer is not eligible for GC
    exactly.



  • got that i need some reading on this topics didnt know about upvalue and newproxy function
    :)
    The upvalues just mean values that are declared outside of the function/block, so if you have something like
     local var = "Test"
    and now if you access this value in a function later, var is called an upvalue as it is not a local variable to that function block.

    The magic in @Atilim's code is about overriding the __gc function via setmetatable, this is when the GarbageCollector is invoked.

    and definitely I am seeing the newProxy function for the first time.
    twitter: @ozapps | http://www.oz-apps.com | http://howto.oz-apps.com | http://reviewme.oz-apps.com
    Author of Learn Lua for iOS Game Development from Apress ( http://www.apress.com/9781430246626 )
    Cool Vizify Profile at https://www.vizify.com/oz-apps
  • @OZApps

    thnx for the great explanation

    :)
Sign In or Register to comment.