• Listen to a special audio message from Bill Roper to the Hive Workshop community (Bill is a former Vice President of Blizzard Entertainment, Producer, Designer, Musician, Voice Actor) 🔗Click here to hear his message!
  • Read Evilhog's interview with Gregory Alper, the original composer of the music for WarCraft: Orcs & Humans 🔗Click here to read the full interview.

got simple question about nulling

Status
Not open for further replies.

Kazeon

Hosted Project: EC
Level 34
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?
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:
Level 19
Joined
Mar 18, 2012
Messages
1,716
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.
 
Level 19
Joined
Mar 18, 2012
Messages
1,716
A leak only occurs if you have data in memory which you can't access anymore.

Both consume the same amount of memory, but World can be accessed at anytime
world is inevitably gone at the end of the function
JASS:
globals
    /*
    *     Maybe can't be set inside the global block and has to be done onInit
    */
    rect Wold = GetWorldBounds()//Increases counter by 1
endglobals
JASS:
function example takes nothing returns nothing
    local rect world = GetWorldBounds()//Increases counter by 1
    set world = null//decreases counter by 1
endfunction
 
Status
Not open for further replies.
Top