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.
  • Ghan Ghan:
    Howdy
  • Ghan Ghan:
    Still lurking
    +3
  • The Helper The Helper:
    I am great and it is fantastic to see you my friend!
    +1
  • The Helper The Helper:
    If you are new to the site please check out the Recipe and Food Forum https://www.thehelper.net/forums/recipes-and-food.220/
  • Monovertex Monovertex:
    How come you're so into recipes lately? Never saw this much interest in this topic in the old days of TH.net
  • Monovertex Monovertex:
    Hmm, how do I change my signature?
  • tom_mai78101 tom_mai78101:
    Signatures can be edit in your account profile. As for the old stuffs, I'm thinking it's because Blizzard is now under Microsoft, and because of Microsoft Xbox going the way it is, it's dreadful.
  • The Helper The Helper:
    I am not big on the recipes I am just promoting them - I use the site as a practice place promoting stuff
    +2
  • Monovertex Monovertex:
    @tom_mai78101 I must be blind. If I go on my profile I don't see any area to edit the signature; If I go to account details (settings) I don't see any signature area either.
  • The Helper The Helper:
    You can get there if you click the bell icon (alerts) and choose preferences from the bottom, signature will be in the menu on the left there https://www.thehelper.net/account/preferences
  • The Helper The Helper:
    I think I need to split the Sci/Tech news forum into 2 one for Science and one for Tech but I am hating all the moving of posts I would have to do
  • The Helper The Helper:
    What is up Old Mountain Shadow?
  • The Helper The Helper:
    Happy Thursday!
    +1
  • Varine Varine:
    Crazy how much 3d printing has come in the last few years. Sad that it's not as easily modifiable though
  • Varine Varine:
    I bought an Ender 3 during the pandemic and tinkered with it all the time. Just bought a Sovol, not as easy. I'm trying to make it use a different nozzle because I have a fuck ton of Volcanos, and they use what is basically a modified volcano that is just a smidge longer, and almost every part on this thing needs to be redone to make it work
  • Varine Varine:
    Luckily I have a 3d printer for that, I guess. But it's ridiculous. The regular volcanos are 21mm, these Sovol versions are about 23.5mm
  • Varine Varine:
    So, 2.5mm longer. But the thing that measures the bed is about 1.5mm above the nozzle, so if I swap it with a volcano then I'm 1mm behind it. So cool, new bracket to swap that, but THEN the fan shroud to direct air at the part is ALSO going to be .5mm to low, and so I need to redo that, but by doing that it is a little bit off where it should be blowing and it's throwing it at the heating block instead of the part, and fuck man
  • Varine Varine:
    I didn't realize they designed this entire thing to NOT be modded. I would have just got a fucking Bambu if I knew that, the whole point was I could fuck with this. And no one else makes shit for Sovol so I have to go through them, and they have... interesting pricing models. So I have a new extruder altogether that I'm taking apart and going to just design a whole new one to use my nozzles. Dumb design.
  • Varine Varine:
    Can't just buy a new heatblock, you need to get a whole hotend - so block, heater cartridge, thermistor, heatbreak, and nozzle. And they put this fucking paste in there so I can't take the thermistor or cartridge out with any ease, that's 30 dollars. Or you can get the whole extrudor with the direct driver AND that heatblock for like 50, but you still can't get any of it to come apart
  • Varine Varine:
    Partsbuilt has individual parts I found but they're expensive. I think I can get bits swapped around and make this work with generic shit though
  • Ghan Ghan:
    Heard Houston got hit pretty bad by storms last night. Hope all is well with TH.
  • The Helper The Helper:
    Power back on finally - all is good here no damage
    +1
  • V-SNES V-SNES:
    Happy Friday!
    +1

      The Helper Discord

      Staff online

      Members online

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top