Unsure about nullifying local vars

Akolyt0r

New Member
Reaction score
33
Hi, i am a bit unsure when i need to nullify local vars, and/or when i should use them
3 possibilites:
JASS:
local integer i=0
local player p
//1: locals, nullify only at the end
loop
   exitwhen i>11
   set p=Player(i)
   call DisplayTextToPlayer(p,0,0,"Some Text")
   set i=i+1
endloop
set p=null

JASS:
//2: locals, nullify everytime i assigned a different value
local integer i=0
local player p
loop
   exitwhen i>11
   set p=Player(i)
   call DisplayTextToPlayer(p,0,0,"Some Text")
   set p=null
   set i=i+1
endloop

JASS:
//3: no locals
local integer i=0
loop
   exitwhen i>11
   call DisplayTextToPlayer(Player(i),0,0,"Some Text")
   set i=i+1
endloop

i know for some types i have defintly choose option 2...(e.g: Locations or Groups), but what about the others ...?
and i know i always should use locals when i use some stuff (e.g. GetTriggerUnit() )quite often ...but what when i use it only one time, should i still use locals, because they leak (do they ?!) ??
 

saw792

Is known to say things. That is all.
Reaction score
280
In that situation, yes. Were there many uses of Player(i) within the loop it would be better to set it to a local.
 

Artificial

Without Intelligence
Reaction score
326
This might not be how it actually works, but you can anyway think like this:
When you make a variable point to a handle, that handle's pointer count is increased by one. When you make the same variable point to something else, the handle's pointer count decreases by one. Handles don't get removed if their pointer count isn't 0.

So, all you need to do (once you've set a variable to point to a handle) is to make it point to some other handle. But when you make it point to that other handle, the other handle's pointer count increases, so you need to make it point to something else again! It's a never ending cycle.

But luckily, there's "null" who can save us. Null is "nothing". So when you make the variable point to null, you make it point to nothing. So there's nothing whose pointer count is increased. :)

So this leads us to at least 2 things:
- you only need to null things when exiting the function (before returns and before the function's end) because the pointer count will be decreased when the variable is set to any other value
- handles that won't be destroyed (e.g. players) don't need to (but can) be nulled as not nulling them just makes them not be removed when they are removed (and naturally things that can't be removed can't be removed anyway :D).

> i know for some types i have defintly choose option 2
Umm. No. :D

> but what when i use it only one time, should i still use locals
Using locals in these situations only makes it more complicated.

> they leak (do they ?!) ??
Locals themselves don't leak.
 

Akolyt0r

New Member
Reaction score
33
> i know for some types i have defintly choose option 2
Umm. No. :D

in your opinion following code would be "good" and would not leak:
JASS:
//your opinion !?
function LocFun takes nothing returns nothing
local location l
local integer i
local rect r=bj_mapInitialPlayableArea
loop
exitwhen i>100
set l=Location(GetRandomReal(GetRectMinX(r),GetRectMaxX(r)),GetRandomReal(GetRectMinY(r),GetRectMaxY(r)))
set i=i+1
endloop
call RemoveLocation(l)
set r=null
set l=null
endfunction


JASS:
//better !?
function LocFun takes nothing returns nothing
local location l
local integer i
local rect r=bj_mapInitialPlayableArea
loop
exitwhen i>100
set l=Location(GetRandomReal(GetRectMinX(r),GetRectMaxX(r)),GetRandomReal(GetRectMinY(r),GetRectMaxY(r)))
call RemoveLocation(l)
set i=i+1
endloop
set r=null
set l=null
endfunction



Players variables dont cause handle leaks anyway. Player handles never get destroyed
what about units ?
 

saw792

Is known to say things. That is all.
Reaction score
280
First one leaks rather hardcore, since you create a new location each time which isn't removed. The only location removed is the very last one.

Second one is alright.

Players don't leak because they are neither created nor destroyed. They are perpetual. Units leak handles when they are not nulled, as they are removed after they decay (and in some other circumstances).
 

Viikuna

No Marlo no game.
Reaction score
265
Units die. I dont know if their handle indexes get recycled automaticly, but I doubt it. Has anyone tested it?

EDIT. So yes, units need to be nulled.
 

saw792

Is known to say things. That is all.
Reaction score
280
Yes they do. That is part of the PUI-solving issue, I believe.
 

Akolyt0r

New Member
Reaction score
33
so i need to null local units vars everytime after i assigned a value to them (e.g. using CreateUnit(.....)) ...or only at the end of a trigger ?
 

saw792

Is known to say things. That is all.
Reaction score
280
You NEVER need to null handles before you set them again. Setting them to a different value already frees the reference to the original handle. You only need to null local handles when you are completely finished with them, at the end of the function (before the return, if a function returns something).
 

Ryuu

I am back with Chocolate (:
Reaction score
64
After you are done with them.

EDIT:
saw792 beat me to it >.>
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Members online

      No members online now.

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top