• 🏆 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] A question on Leaks

Status
Not open for further replies.
Level 4
Joined
Nov 24, 2007
Messages
55
So I have the following code:

JASS:
function Trig_Boss_FindLowestHP takes group g returns unit
    local unit u
    local unit lowest
    loop
        set u = FirstOfGroup(g)
        exitwhen ( u == null )
        // we know U is not null...
        if( lowest == null ) then
            set lowest = u
        else
            if( GetUnitLifePercent(u) < GetUnitLifePercent(lowest)) then
                set lowest = u
            endif
        endif
        call GroupRemoveUnit(g, u)
    endloop
    set u = null
    return lowest
endfunction

and it is called by the following line of code

JASS:
local unit target = Trig_Boss_FindLowestHP(g)

My question is...at the end of my iteration through the group, do I need to destroy it and set the variable (g, as was handed by the other call) to null? I know i could to be safe (and I don't use the group after this point...but what if I did? Is this pass by value or pass by reference, i assume the latter...)

Thanks
 
Level 23
Joined
Nov 29, 2006
Messages
2,482
As far as I know you should never destroy a passer (like this group). If you destroy it it could lead to references issues when the function is called next time.
If you want to destroy it, do so after the call. I mean

JASS:
local unit target = Trig_Boss_FindLowestHP(g)
call DestroyGroup(g)
set g = null

Also, you should do local unit lowest = null in the first place. An undeclared variable is not null unless you dont set it as null.
set u = null is not needed either, since if the unit group is full, and you try to find the firstofgroup, it will result to u = null.
 
Level 4
Joined
Aug 9, 2004
Messages
70
Nice question TigerShark, nice answer Eccho.

I'll try another approach.

Don't you think that you need to nullify that lowest at the end of this function? If not, why?

The answer is simple, because you can not. When the function meets return, it finishes the function by returning a value, and skips the remaning actions. So there must be a solution because you could be returning some really important variables that may cause great leaks. (Sounds, effects, groups, forces...)

Let's look at this example.

JASS:
function aFunction takes unit a returns nothing
   set a = null
endfunction

function anotherFunction takes nothing returns nothing
   local unit b = CreateUnit(..)
   call aFunction(b)
   call SetUnitX(b, 0)
   call SetUnitY(x, 0)
endfunction

I don't know the results of these, (I can't test it right now.)
But what I think is it wont set x,y to 0,0. Because we already cleaned the adress of that unit. So, in your example, what you have to do is just what Eccho said.

Wish I could helped.
 
Last edited:
Level 23
Joined
Nov 29, 2006
Messages
2,482
Well if you do the following:

JASS:
function hmm takes group s returns nothing
 call DestroyGroup(s)
endfunction
function something takes nothing returns nothing
 local group g = CreateGroup()
 call hmm(g)
endfunction

Wouldnt this destroy the group g too?

Oh right, ofcourse, they are pointers to the same handle. I see what you mean

Edit: then, I said the wrong thing about my first reply I think ? >.<
 
Level 4
Joined
Aug 9, 2004
Messages
70
I think it destroys. Actually I tried it. :D
I guess the reason is when you null a pointer, you get rid of that address, it doesn't harm your unit or group.
But when you call a function like DestroyGroup(g), you go to that address, and destroy what is inside! So you harm them. Destroying a group is similar with killing a unit, removing a lightning effect.


JASS:
function hmm takes group s returns nothing 
call DestroyGroup(s)
set g = null
endfunction
function something takes nothing returns nothing 
local group g = CreateGroup() 
call hmm(g)
endfunction


JASS:
function hmm takes group s returns nothing 
call DestroyGroup(s)
endfunction
function something takes nothing returns nothing 
local group g = CreateGroup() 
call hmm(g)
set g = null
endfunction


Is that correct Purple?
 
Status
Not open for further replies.
Top