English Do timers ever automatically free

4 replies
Goto Page
To the start Previous 1 Next To the start
Up
Vehk
User
Offline Off
Are timers automatically freed when they expire? Or do they always need to be freed manually?
02.08.17 04:14:57 am
Up
Talented Doge
User
Offline Off
No.
Fus~Ro Dah! | Check out Lord Sheogorath's great file cs2d THE WABBASNOWBALL! (11)
09.08.17 04:38:31 pm
Up
Flacko
User
Offline Off
I don't think you need to free a timer that has expired for cleanup purposes, at least it doesn't state so in the help page: cs2d lua cmd timer.
It kinda makes sense that CS2D handles this for you. How else would you be supposed to know when to free the unused resources?
You couldn't use a second timer, but you would need a third one to free the resources from the second and so on...
It wouldn't be good design to free the timer in your callback function either, because that would make the function too specific and would require some way of counting ticks in the lua side too.

EDIT: I've just tested your custom timer functions and it seems there's a bug regarding this.
Given the following snippet:
Code:
1
2
timer(1000, 10, function() msg('fx') end)
timer(1500, 5, function() msg('fy') end)

Your timer implementation seems to free all the timers when the first one finishes. Could this also be a bug in CS2D?
Removing the oldfreetimer call in your freetimer() implementation solves this, but you won't be able to use freetimer() to free timers prematurely
edited 1×, last 09.08.17 06:26:19 pm
12.08.17 02:38:42 pm
Up
Vehk
User
Offline Off
You are probably right that CS2D does it for you. I got the idea that it didn't from the warning from cs2d lua cmd timer probably was just me being a little too paranoid

Thanks for the bug find! I did some further testing and found that it wasn't freeing all timers, but something else was wrong.

Code:
1
2
3
timer(1000, 10, function (arg) arg.x = arg.x + 1; msg('X' .. arg.x) end, {x = 0})
timer(1500, 5, function (arg) arg.x = arg.x + 1; msg('Y' .. arg.x) end, {x = 0})
timer(2000, 5, msg, "Z")


The output from above

More >


After the call to freetimer the other two timers continue, but the first timer seems to end early. I was stumped until I looked at this line in timer.lua

Code:
1
timer(ms, fullname, "", count)


Got the idea that maybe the timer is freed once it's time is up but before the final call. After changing that line to this

Code:
1
timer(ms, fullname, "", 0)


I get the expected results (all output, and all 3 debug messages about the timers being freed).

That solved the problem, but what caused it in the first place?

I'll look more into that when I have time.
edited 1×, last 12.08.17 02:49:32 pm
13.08.17 12:09:52 pm
Up
DC
Admin
Offline Off
CS2D frees timers as soon as they expire. It would not make any sense to keep them because there's no way to do anything with them once they expired.

They are basically lightweight throw away thingies. Create, use, throw away.

That warning is there because you have to keep in mind that you can potentially run a lot of code frequently using timers which of course can slow down the game. The timers themselves do not add significant overhead however.
www.UnrealSoftware.de | www.CS2D.com | www.CarnageContest.com | Use the forum & avoid PMs!
To the start Previous 1 Next To the start