• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!

[JASS] local variable destruction

Status
Not open for further replies.
Level 7
Joined
Jul 20, 2008
Messages
377
Nothing is automatically destroyed. You must manually invoke the "DestroyGroup", "RemoveLocation" and so on forth.

Additionally, for any local variable that is of type handle or extends handle, you must set them to null at the end of your functions.
 
Level 29
Joined
Jul 29, 2007
Messages
5,174
Local variables are only declared inside their scope.

And not setting pointers to null (point to 0), the next time you use them, seeing as you destroyed what they pointed to - they can point to something else, which can mess your program.

Now that I've said that I am not really sure it's necessary to set pointers to null at the end of their scope as they are deleted anyway... anyone knows more then me?
 
It just messes up the handle stack. Say you created a local location l, and it's handle id was 0x100000+1 (in case you're wondering, 0x100000 is the hex number at which the handle stack starts), then you nulled it at the end of the function, then ran the function again before creating any more handles, the new local variable's handle id would also be 0x100000+1, whereas if you hadn't nulled it, the new location's id would be 0x100000+2. It's no big deal, it doesn't cause any memory leaks, but the problem comes when the handle stack reaches its limit. I'm not sure what happens then, but it can't be good.

I hope I explained it well enough, feel free to ask questions if you didn't understand something.
 

Dr Super Good

Spell Reviewer
Level 64
Joined
Jan 18, 2005
Messages
27,202
Acording to current belief, No local leaks data. The memory the local used to store a value is removed when the fnction ends. What can happen thou is that an object a local was refererencing leaks.

This happens when you do not destroy it and it can no longer be easilly fetched to be destroyed. However due to bad garbage collectiors in the WC3 engine, what can happen is an object still things a local is referencing it when it actually is not, even if the object is destroyed and so blocks up a handle index permantly.

Thus you only need to null local handles that use values that are not constant or not refrencable. Eg you do not need to null a local player as there are only 16 of them which all can be fetched via triggers (return the same handle index) thus it does not mater if they can never be freed up.

Equally, if you use object recycling, they do not need to be nulled as the objects are constantly reused and never destroyed so their indexes never need to be recycled.
 
Level 7
Joined
Jul 20, 2008
Messages
377
Object recycling is simply where you reuse the object over and over. For example, consider an unit group. To recycle an unit group would be to use a global unit group and simply clear it, as opposed to destroying it.

Or timers; you could recycle timers by just pausing them and then restarting them at a later time as opposed to destroying a timer and creating a new one.
 
Level 13
Joined
Mar 16, 2008
Messages
941
First destroy it, then null it.
A local variable is never a location or a group. It's a pointer to a group, a reference to it. If you give a pointer a new value (null) it's not possible to destroy the "old" handle (unless you have another variable pointing at it, but that should be clear).
First you need to destroy it, for example "call RemoveLocation(tempLoc)" removes the location at which "tempLoc" is pointing at. Now you fixed the leak, but the variable still has the reference to a destroyed object, so you should null it :)
 
Level 8
Joined
Aug 4, 2006
Messages
357
JASS:
set udg_movePoint[pickedPlayerId] = lastCreatedUnitPosition
...
call RemoveLocation(lastCreatedUnitPosition)
set lastCreatedUnitPosition = null

so if i'm understanding what Justify said correctly, i should not RemoveLocation(lastCreatedUnitPosition) in the above case because it will cause udg_movePoint[pickedPlayerId] to point to nothing. this would keep me from using the value of udg_movePoint in future triggers.
right?
 
Level 7
Joined
Jul 20, 2008
Messages
377
First destroy it, then null it.
A local variable is never a location or a group. It's a pointer to a group, a reference to it. If you give a pointer a new value (null) it's not possible to destroy the "old" handle (unless you have another variable pointing at it, but that should be clear).
First you need to destroy it, for example "call RemoveLocation(tempLoc)" removes the location at which "tempLoc" is pointing at. Now you fixed the leak, but the variable still has the reference to a destroyed object, so you should null it :)

No, if a variable is local, that doesn't mean it's a pointer. You're thinking of a handle variable type, which is a pointer.

The local keyword just indicates the scope of the variable and nothing more than that. However, there IS a handle index leak issue (which is explained elsewhere if you use the search engine), so you have to null local handle variables.

But locals such as real, integer, string... basically anything that's not a subtype of handle, you don't have to null.
 
Status
Not open for further replies.
Top