• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • It's time for the first HD Modeling Contest of 2024. Join the theme discussion for Hive's HD Modeling Contest #6! Click here to post your idea!

null timers?

Status
Not open for further replies.
Level 23
Joined
Apr 16, 2012
Messages
4,041
Well, I made a spell using timer(s) and i nulled them after starting.
But recently I've read this:
no problem
Note, that destroying timers and then setting its variable to null can cause a similar handle stack corruption bug. You can avoid this by using timer recycling, for example TimerUtils (or by never setting timer variables to null, taking insignificant leaks into account).

So if I do this:
JASS:
function A takes nothing returns nothing
    local timer t = GetExpiredTimer()
    //some other stuff here
    set t = null
endfunction

function B takes nothing returns nothing
    local timer t = CreateTimer()
    call TimerStart(t, 1., false, function A)
    set t = null
endfunction
Is both t = null OK or shouldn't I null timers?
 
Level 17
Joined
Feb 11, 2011
Messages
1,860
From what I understand, they are saying that destroying timers is bad, not nulling the timer variables.

Your script is okay, but the timer is leaked (it is not destroyed or nulled). I recommend using TimerUtils to recycle timers, as mentioned in the quote.
 
If you worry about the handle stack corruption then you can destroy the timer but not null it.

I've never encountered that bug that way, although I am sure it exists. However, it most likely requires some specific functions to be called like the handle stack corruption that comes with trigger sleep action.

But as the quote and mrbean said, you can always just use a timer recycling system and not worry about it.
 
Level 23
Joined
Apr 16, 2012
Messages
4,041
So I understand I should use TimerUtils, but if i want it in vanilla JASS? will
JASS:
function A takes nothing returns nothing
    local timer t = GetExpiredTimer()
    //some other stuff here
    call DestroyTimer(t)
    set t = null
endfunction

function B takes nothing returns nothing
    local timer t = CreateTimer()
    call TimerStart(t, 1., false, function A)
    set t = null
endfunction

may cause bugs?(as the quote states)
 
Level 17
Joined
Apr 27, 2008
Messages
2,455
The above script should be safe from Handle Stack corruption
Handle Stack corruption only occurs in rare cases, trust me.

I take advantage of handle stack corruption to get access to unit shadows in-game.
It's really cool actually ;3

Well it's just because images, texttags and maybe some other handles that can safely be created in a local block are not handled in the same way.
The id is recycled once the handle has been destroyed, no matter if some variable still points on it or not. (at least they are not concerned by the "null" bug ;) )

This is an example of handle stack corruption (if it's still not fixed)
 

Dr Super Good

Spell Reviewer
Level 64
Joined
Jan 18, 2005
Messages
27,201
It involves the garbage collector having a reference counter leak. Objects increment the reference counter when referenced but if the local that references them is left to expire it does not dereference them. Not all objects are prone to that kind of leak as objects like Players do not get garbage collected (they are constants).
 
Level 29
Joined
Mar 10, 2009
Messages
5,016
This is madness ;_;
no, this is BLASPHEMY from 300 Spartans XD...

Not destroying timers always leaks, TimerUtils always creates timer handles if the previous
timercreated is still in use therefore creating another timer, which causes another memory allocation,
although not really a big deal but what do you think of 256 or more timers created at map initialization
and just recycle it?...
 

Dr Super Good

Spell Reviewer
Level 64
Joined
Jan 18, 2005
Messages
27,201
It is generally a good idea to recycle objects. The practice is called "Green Coding" and can actually make code execute faster in real programs due to the reduction of memory allocation and de-allocation overhead.

That said, it is a good idea to implement it in a sane way. Be aware of possible spike conditions which could lead to excessive permanent allocation unless handled correctly. Consider using bounded structures which will only start destroying objects when full, giving enough time for objects to be destroyed safely.

Green Coding is useless if every single system keeps their own object buffer. In WC3 object buffers should be global and used in all systems that require their functionality. In Real programming it can sometimes be a good idea to use thread based object buffers to avoid synchronization overhead although such buffers should logically be smaller than global ones.
 
Status
Not open for further replies.
Top