• 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.

Do I need to destroy a variable after use?

Status
Not open for further replies.
Level 7
Joined
Jul 1, 2008
Messages
1,025
Hi, I am abit confused, I have been told you need to store data in variables to stop it leaking and causing lag/crashes etc, so for example if you are selecting every unit owned by player of type, you need to store it in a variable. Thats fine, I understand that.

But do I then need to destroy that variable after I've used it to stop it leaking? And if so do I need to do this for all variables types?
 
Level 33
Joined
Mar 27, 2008
Messages
8,035
If your variable is local (done in JASS Script), you must null it if it's a handle (Units, Locations, etc).

Example;
  • Custom script: local unit u = GetTriggerUnit()
  • Custom script: call KillUnit(u)
  • Custom script: set u = null
Only null this if you're creating a local variable (Handle-type).
Variables such as Integer/Real, does not need to be nulled as they are not handles.

But I can't say for sure should we first null location, or destroy them first.
Either way, it'll work (I think).
 
Level 20
Joined
Jul 6, 2009
Messages
1,885
If your variable is local (done in JASS Script), you must null it if it's a handle (Units, Locations, etc).

Example;
  • Custom script: local unit u = GetTriggerUnit()
  • Custom script: call KillUnit(u)
  • Custom script: set u = null
Only null this if you're creating a local variable (Handle-type).
Variables such as Integer/Real, does not need to be nulled as they are not handles.

But I can't say for sure should we first null location, or destroy them first.
Either way, it'll work (I think).

Globals need to be nulled too, it's just that nobody does it.

You need to destroy the location before nulling it, because nulling removes the reference to the location.

Ok I don't really understand all this null variable stuff, I was never pro at triggers but I will ask my triggerer friend if he can have a look at this thread and get rid of any leaks in our map, hope he will understand!

As far as I know, variables of handle type point to the address of data instead of holding it (which would be logical, because handles are structures). Because they're being referenced by variables, a handle will not be recycled until all of the references are gone. By nulling a variable, you remove the reference and the handle is free to be recycled.
 
Level 17
Joined
Nov 13, 2006
Messages
1,814
Globals need to be nulled too, it's just that nobody does it.

You need to destroy the location before nulling it, because nulling removes the reference to the location.

but since u use more time the variable and location is removed by custom scripts its ok if u store different reference afyer last was solved with custom script (RemoveLocation/DestroyForce,DestroyGroup etc)
 
Level 10
Joined
Mar 17, 2012
Messages
582
Hi, I am abit confused, I have been told you need to store data in variables to stop it leaking and causing lag/crashes etc, so for example if you are selecting every unit owned by player of type, you need to store it in a variable. Thats fine, I understand that.

But do I then need to destroy that variable after I've used it to stop it leaking? And if so do I need to do this for all variables types?

If your data changes during the game - you should destroy unit groups/remove locations e.t.c... but if not - just leave it be ^_^
 
Status
Not open for further replies.
Top