HomeUser Control Panel (unavailable in archive)ForumsTutorialsArt GalleryResourcesMaps

Destroying Handles

04-24-2006, 12:58 AM#1
The_AwaKening
I'm just questioning whether I am destroying groups and removing locations correctly.

Lets just say that I am using
local group G

I then set G=GetUnitsOfTypeIdAll('h001')
I call ForGroup( g, function whatever )

Later in that same function, I set G=somethingelse and call ForGroup again.

I have just been calling DestroyGroup(G) at the end of the function and not destroying it each time. Do I instead need to destroy the group each time before I set it to something else? Same with other handles.
04-24-2006, 02:27 AM#2
Vexorian
handle typed variables in JASS are pointers, so a variable never holds the object itself but a reference to an object.

The rule to destroy an object is to do so before all references to it are lost. Most of the times it is before the end of a function and before re-assigning the only variable that points to it to another object
04-24-2006, 02:52 AM#3
The_AwaKening
Just to be sure I'm clear in what you are saying. I need to destroy G everytime before I reassign it.

Collapse JASS:
function GroupTest takes nothing returns nothing
    local group G

    set G=GetUnitsOfTypeIdAll('h001')
    call ForGroup( g, function test )
    call DestroyGroup(G)
    set G=GetUnitsOfTypeIdAll('h002')
    call ForGroup( g, function test )
    call DestroyGroup(G)

    set G=null
endfunction
04-24-2006, 10:30 AM#4
Earth-Fury
Quote:
Originally Posted by The_AwaKening
Just to be sure I'm clear in what you are saying. I need to destroy G everytime before I reassign it.

Collapse JASS:
function GroupTest takes nothing returns nothing
    local group G

    set G=GetUnitsOfTypeIdAll('h001')
    call ForGroup( g, function test )
    call DestroyGroup(G)
    set G=GetUnitsOfTypeIdAll('h002')
    call ForGroup( g, function test )
    call DestroyGroup(G)

    set G=null
endfunction

a hanel is a pointer. like "X marks the spot" on a buried tresure. Without the X, the tresure is still there, it just has nothing pointing to it.

So, would you need to destroy a group before its "X" is gone? (Yes.)
04-24-2006, 02:54 PM#5
The_AwaKening
Thanks guys, I have some memory cleanup to do I guess.

I do have one more question though. Here is a trigger I use that call GroupEnumUnits in a loop. When using that function, I'm assuming that I don't need to destroy the group until the end?
Collapse JASS:
function ReviveAllBool takes nothing returns boolean
    return GetOwningPlayer(GetFilterUnit())==GetTriggerPlayer() and GetUnitTypeId(GetFilterUnit())=='ewsp'
endfunction

function Trig_revive_all_Actions takes nothing returns nothing
    local player p=GetTriggerPlayer()
    local group g=CreateGroup()
    local integer i=1
    local unit u
    local integer j=GetConvertedPlayerId(p)
    local integer k=( ( ( ( j - 1 ) - ModuloInteger(( j - 1 ), 3) ) / 3 ) + 1 )
    local real wait=0.30
    local location l=GetRectCenter(gg_rct_dead_hero_revive_1)
    local boolexpr b
    local location area=GetRectCenter(gg_rct_dead_hero_spawn_1)
    local real range

    set b=Condition(function ReviveAllBool)
    set range=800
    call GroupEnumUnitsInRangeOfLoc(g,area,range,b)
    loop
        exitwhen CountUnitsInGroup(g)==0 or i>35 or udg_gameover or udg_teamLost[k]
        set u=GroupPickRandomUnit(g)
        call SetUnitPositionLoc(u,l)
        call TriggerSleepAction(wait)
        call GroupEnumUnitsInRangeOfLoc(g,area,range,b)
        set i=i+1
    endloop

    call RemoveLocation(l)
    call RemoveLocation(area)
    call DestroyGroup(g)
    call DestroyBoolExpr(b)
    set p=null
    set g=null
    set u=null
    set l=null
    set area=null
    set b=null
endfunction

//===========================================================================
function InitTrig_revive_all takes nothing returns nothing
local integer i=0
    set gg_trg_revive_all = CreateTrigger(  )
    loop
        exitwhen i>8
        call TriggerRegisterPlayerChatEvent( gg_trg_revive_all, Player(i), "-ra", true )
        call TriggerRegisterPlayerChatEvent( gg_trg_revive_all, Player(i), "-revive", true )
        set i=i+1
    endloop
    call TriggerAddAction( gg_trg_revive_all, function Trig_revive_all_Actions )
endfunction
04-25-2006, 01:07 AM#6
The_AwaKening
Another question associated to this trigger would be, does GroupEnumUnits add to the group, or does it clear and then regroup it?