- Joined
- Oct 12, 2011
- Messages
- 3,449
I'm always confused about nulling locals. actually, when must I null them? what kind of local that I need to null after use? If I have this local, am I need to null it?
I set the wb after declaration because there must be some condition to use the wb.. the question is still the same...
answer
JASS:
local rect wb
set wb = GetWorldBounds()
//As example I display the max X as texp
call DisplayTextToPlayer(Player(0), 0.0, 0.0, R2S(GetRectMaxX(wb)))
// then am I need to null the wb?
I set the wb after declaration because there must be some condition to use the wb.. the question is still the same...
answer
All agent types are reference counters. Aslong as an agent reference count is not 0 the handle id and memory can't be freed.
Whenenever a variables is set to point to an agent the counter increases by one, once it points to something else (i.e = null) the counter decreases by one.
Naturally locals should decrease the counter automatically once they disappear, but they don't. When they disappear they leave an unfixable memory leak, because the counter will never go back to 0.
That's why you need to null nearly every agent type variable.
What type of variables don't create memory leaks at all are
- Strings
- Integers
- Reals
- Booleans
As exception player and playerstate, because their handle id won't be freed in any case, it still doesn't hurt to null them. It's good practise.
Now to your example.
local rect wb must be nulled at the end of the function or before a return.
For GetWorldBounds you should use a library like "WoldBounds" where GetWorldBounds() is set once to a global rect variable.
You can access it at anytime via WorldBounds.world or World if it is not a struct.
JASS:
function example takes nothing returns nothing
local rect world = GetWorldBounds()//Increases counter by 1
set world = null//decreases counter by 1
endfunction
Last edited: