HomeUser Control Panel (unavailable in archive)ForumsTutorialsArt GalleryResourcesMaps

leak question

01-06-2008, 06:15 AM#1
Xinlitik
I'm a little confused as to whether this will leak or not...

Collapse JASS:
set bj_wantDestroyGroup = true
set u = GroupPickRandomUnit(GetUnitsInRectMatching(gg_rct_Recycle, Condition(function UnitCriteria)))

Should I instead do it this way? --

Collapse JASS:
set ug = GetUnitsInRectMatching(gg_rct_Recycle, Condition(function UnitCriteria))
set bj_wantDestroyGroup = true
set u = GroupPickRandomUnit(ug)
set ug = null

thanks
01-06-2008, 06:20 AM#2
xombie
Collapse JASS:
GetUnitsInRectMatching(...)
This creates a group "object", and thus needs to be destroyed. You were on the right track, but in order to properly clean leaks you must do this:
Collapse JASS:
local group g = CreateGroup()
local unit u

call GroupEnumUnitsInRect(g, gg_rct_Recycle, Filter(function UnitCriteria))
set u = GroupPickRandomUnit(g)

call DestroyGroup(g)
set g = null
I believe that would be the most efficient way of doing it (ignoring the GroupPickRandomUnit). Remember, at some point you will also need to null the variable u. You should look for a tutorial on leaks.

*If you didn't catch it, notice the DestroyGroup call, that is very neccessary.
01-06-2008, 06:28 AM#3
Xinlitik
Thanks for the reply.

Here's where I am confused: GetUnitsInRectMatching says that it returns its group--at which point, it gets sent to Pickrandomunit etc, which destroys the group as part of its action.... is the group that gets returned by GetUnitsInRectMatching a *new* copy of the group created inside the function, or is it the same one, in which case it would get destroyed later.

Edit: this is what i mean

Collapse JASS:
function GetUnitsInRectMatching takes rect r, boolexpr filter returns group
    local group g = CreateGroup()
    call GroupEnumUnitsInRect(g, r, filter)
    call DestroyBoolExpr(filter)
    return g //is this group that is sent to my function a copy of the group g, or is it the actual group g?
endfunction

Also, I noticed that you didn't destroy the boolean filter like the blizzard function--is that because Filter( ) is different from Condition( )?


OK all new version, what do you think? The part in the middle is in a loop in which it must pick a different kind of unit every time.

Collapse JASS:
local group ug = CreateGroup()
...
...
call GroupEnumUnitsInRect(ug, gg_rct_Recycle, Filter(function UnitCriteria))
set u = FirstOfGroup(ug)
call GroupClear(ug)
...
...

call DestroyGroup(ug)
set ug = null

01-06-2008, 07:08 AM#4
xombie
To answer your first question, it is the same group.

To answer your second question(s), Filter( ) is the same as Condition( ) and boolexprs do not leak, therefore do not need to be removed.

Lastly, looks all good. Great job.
01-06-2008, 07:10 AM#5
Xinlitik
Thanks a bunch for the help
01-06-2008, 07:34 AM#6
xombie
Not a problem.