HomeUser Control Panel (unavailable in archive)ForumsTutorialsArt GalleryResourcesMaps

Unit Variables... Leak?

07-21-2007, 10:29 PM#1
Tide-Arc Ephemera
Do unit (not unit type) variables leak? If so, can somebody tell me how to um... unleak them?

I've searched everywhere, but where it says unit... it basically is just "create unit on point" instead of how to fix unit leaks.
07-21-2007, 11:14 PM#2
Toadcop
ANY variable is not leaking... wait a bit so you get it explained. ^^
07-21-2007, 11:19 PM#3
Naakaloh
What Toadcop means is, there are methods for handling all variables carefully enough that they should not leak... except for trackables.

There is a RemoveUnit function and if you assign a unit to a local variable, you must assign null to that variable after removing the unit.
07-21-2007, 11:27 PM#4
Tide-Arc Ephemera
Trigger:
Set TempUnit = (Target unit of ability being cast)

Does that leak? If so, how can I clean it... I'm having a blond day, more blond than usual...
07-21-2007, 11:30 PM#5
Toink
If you use that global more than once, it won't leak.

Things only leak if you set a variable's value to something, use the variable once, and never use that variable again and forgetting to clean it.

Globals often don't leak, but locals do.
07-21-2007, 11:32 PM#6
darkwulfv
Making a (local) unit variable, if it's not a unit to be removed (like a hero), just null the variable. otherwise, if it's a dummy unit for example, use RemoveUnit((your unit variable)), and null it.
07-22-2007, 12:02 AM#7
Earth-Fury
if you create something (CreateUnit()) you must destroy it (RemoveUnit()). JASS also is broken in the sense that destroying something will not work if it is still in use. (if a variable is still pointing to it) locals do not stop pointing at something implicitly at the end of a function. globals would also suffer from this, but who cares, as their value will eventually be changed, destroying the remaining reference to the object in question.

to understand this well, first you must understand handles. which im too lazy to explain this second.
07-22-2007, 12:31 AM#8
Tide-Arc Ephemera
One last question... nobody ever told me this but what's the difference between locals and globals?
07-22-2007, 12:53 AM#9
Rising_Dusk
Quote:
if you create something (CreateUnit()) you must destroy it (RemoveUnit()).
In this case, that is not true.
Units remove themselves after decaying, so killing is equally okay for a unit to not leak.

Globals are variables that are just that... Global.
They can be used in any function anywhere. Locals are specific to the function in which they are declared (Unless moved to other functions via globals or some other method).
07-22-2007, 12:54 AM#10
Tide-Arc Ephemera
So... Globals are the variables made in the Variable Editor, and locals aren't the variables made in the Variable Editor?
07-22-2007, 12:56 AM#11
Rising_Dusk
Quote:
So... Globals are the variables made in the Variable Editor, and locals aren't the variables made in the Variable Editor?
Yes.
07-22-2007, 01:03 AM#12
Anitarf
Collapse JASS:
globals
    integer a
endglobals

function foo takes nothing returns nothing
    local integer b
    set a = 3 
    set b = 4
endfunction

function bar takes nothing returns nothing
    set a = 5
    set b = 6
    call foo()
    call BJDebugMsg(I2S(a)+I2S(b))
endfunction
If you call the function bar, the message displayed will be "36". Function bar sets a to 5 but it is a global variable, so function foo has access to it too and once function bar calls function foo, a gets set to 3. Variable b is a local variable in both functions, whatever function foo does with it's b does not affect the value of b in bar. This isn't only true between different functions, but also calls to the same function:
Collapse JASS:
function foo takes nothing returns nothing
    local integer b = GetRandomInt(1, 100)
    call TriggerSleepAction(5.0)
    call BJDebugMsg(I2S(b))
endfunction

function bar takes nothing returns nothing
    call ExecuteFunc("foo")
    call TriggerSleepAction(1.0)
    call ExecuteFunc("foo")
    call TriggerSleepAction(1.0)
    call ExecuteFunc("foo")
endfunction
Each execution of foo will create an instance of it with it's own local variables, so while the first foo is waiting it's five seconds the other two that are executed in the meantime won't alter it's b, so in the end the three numbers that get displayed will not be the same (unless you're very lucky and all random checks give the same number).

I hope this illustrates the difference between locals and globals well enough.
07-22-2007, 01:11 AM#13
Earth-Fury
lets introduce you to the concept of scope.

in jass, there are 2 scores for variables, 1 for functions. things only exist in the scope they are created in, and "child" scopes of that. for example:
Collapse JASS:
globals
    integer i = 5
endglobals

function abc takes nothing returns nothing
    local integer k = i
endfunction

function def takes nothing returns integer
    return i
endfunction

the variable "i" (a global) exists in all functions. the variable "k" (a local) exists only in the function "abc". paramiters (what a function "takes") are esentally locals.

functions exist in one scope, the global scope. you can't do
Collapse JASS:
function abc takes nothing returns integer
    function def takes nothing returns integer
        return 4
    endfunction
    return def()
endfunction
its invalid, because functions can exist only in the global scope. (they can be used anywhere)
07-22-2007, 04:06 AM#14
Tide-Arc Ephemera
I see... thanks for the clarification, +Rep to all those who put time into helping.