Why to null local variables? [question]

SerraAvenger

Cuz I can
Reaction score
234
Wandering through this forum, I often saw ppl consisting on a

[any local variable here] = null

at whether the end of the function or as soon as the variable got useless...

But I do not get why. The only possible explanation might be an "undefined" result when using that variable again afterwards - but I don't really see the advantage of "Not working" towards "undefined". And my local variables are destroyed anyways... Neither do I use it afterwards. Especially "nulling" at the end of a foo seems absolutely pointless to me...

And no I don't want the help of any semi-pro's that tell me their opinion and not how it is. And no, I don't judge pro / not pro from the number of green buttons right to your name ( aka reputation ) ( which doesn't mean that I don't give + rep for anyone helping me^^ )

Ty in advantage Davey Jones
 

Tinki3

Special Member
Reaction score
418
http://www.thehelper.net/forums/showthread.php?t=62304

You should nullify a local variable after you've finished using it.
That way your telling the trigger "yup, I'm done with that variable".
Otherwise you'll end up with a leaky-trigger, as a new, fresh variable is created each time the trigger is run.
 

SFilip

Gone but not forgotten
Reaction score
633
> And my local variables are destroyed anyways
Before this happens the game checks if the variable still points somewhere.
Since Destroying/Removing doesn't actually remove the contents but rather marks the space as unoccupied, the game thinks something is still there and keeps the variable.
Nulling assures that the game knows this specific variable is not being used anymore and that's where the actual removal happens.
Of course variables that will never get destroyed do not need to be nulled - these include, but aren't limited to, boolexprs, players and heroes (unless you remove them with RemoveUnit at some point).

Well this is basically the same as AceHart's explanation in that thread. Wish I saw that before I actually wrote this... :p
 

PurgeandFire

zxcvmkgdfg
Reaction score
509
http://www.thehelper.net/forums/showthread.php?t=62304

You should nullify a local variable after you've finished using it.
That way your telling the trigger "yup, I'm done with that variable".
Otherwise you'll end up with a leaky-trigger, as a new, fresh variable is created each time the trigger is run.

Yes, and a big, hairless, monster will come to eat you or do whatever he wants with you. :p

Like Tinki's avatar, it took over him a while ago.
 

SerraAvenger

Cuz I can
Reaction score
234
> And my local variables are destroyed anyways
Before this happens the game checks if the variable still points somewhere.
Since Destroying/Removing doesn't actually remove the contents but rather marks the space as unoccupied, the game thinks something is still there and keeps the variable.
Nulling assures that the game knows this specific variable is not being used anymore and that's where the actual removal happens.
Of course variables that will never get destroyed do not need to be nulled - these include, but aren't limited to, boolexprs, players and heroes (unless you remove them with RemoveUnit at some point).

Well this is basically the same as AceHart's explanation in that thread. Wish I saw that before I actually wrote this... :p

Thank you!
I assumed that the variable is destroyed in any case, thus I saw no point in nulling them.

Greetings Davey
 
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