Leaking? [just read something that put doubts in my mind about this function]

ZakkWylde-

New Member
Reaction score
14
1. If I call this function 5 times, with numPatrols at a constant value:

JASS:
function MakeSpinUnit takes integer numPatrols, rect r returns nothing //removed integer v
    local integer yi
    local real x = GetRectCenterX(r)
    local real y = GetRectCenterY(r)
    local integer v
    local integer index = 0
    
    set numberCirclePatrols = numPatrols
    
    loop
    exitwhen index == numPatrols
        set yi = GetRandomInt(0, 4)
        if yi == 0 then
            set v = 'n000'
        elseif yi == 1 then
            set v = 'n001'
        elseif yi == 2 then
            set v = 'n002'
        elseif yi == 3 then
            set v = 'n003'
        elseif yi == 4 then
            set v = 'n004'
        endif
    
        set spinUnit[index] = CreateUnit(Player(PLAYER_NEUTRAL_AGGRESSIVE), v, x, y, 0)
        call SetUnitVertexColorBJ(spinUnit[index], mRed, mGreen, mBlue, 0)
        call TriggerRegisterUnitInRange(gg_trg_Kill, spinUnit[index], uC, null)
        call GroupAddUnit(udg_EnemyUnits, spinUnit[index])
        call SetUnitFlyHeight(spinUnit[index], 0, 99999)
        
        set index = index+1
    endloop
    
endfunction


will this leak? (4 times "numPatrols" units?)

2. After calling this 5 times, would it be possible just to remove all units in udg_EnemyUnits (and then remove those units from the group) and thereby dispose of any leaks?

3. IF #2: If I remove the units udg_EnemyUnits and then remove them from the group udg_EnemyUnits each time before calling the function again, will it still leak?

4. Is anything leaking if I just call this once?

5. Is it better, in terms of performance, to set u = spinUnit[index] and use u in all places instead of spinUnit[index], and then at the end of the function, set u = null?

<3
Zakk

EDIT: If you know an answer to one of these, lemme know :D. [don't worry about not answering all of them at once. :p]
 

huanAk

New Member
Reaction score
4
1, can't find any leak with my average JASS skill

2, your funcion didn't leak, as long as you never call "set udg_EnemyUnits = CreateGroup()"

3, yes, you should empty group before call this function, so old unit won't mix with new

4, NO... if you don't use udg_EnemyUnits anymore, then try destory it, because it will take up your memory address for whole game (hence "leak" once). but if you still use it, then don't need to destory

5, yes local is "slightly" faster than global. especial if you have hundred of difference global which take longer to lookup.
 

ZakkWylde-

New Member
Reaction score
14
In response to your answer to 1, --
I was wondering if the units created that were originally referred to by spinUnit[index] would leak once spinUnit[sameIndexAsBefore] creates and is SET to another unit...
(or if those "potential" leaks could then be cleaned up by locating them through the group udg_EnemyUnits)
 

huanAk

New Member
Reaction score
4
Set variable to another unit do not leak.

because it still have same amount of memory happen.

unlike CreateGroup(), it allocate new memory block, so it will leak if you don't clear old group (release old memory block).
 

Bribe

vJass errors are legion
Reaction score
67
call TriggerRegisterUnitInRange(gg_trg_Kill, spinUnit[index], uC, null)

>> This is going to leak until you destroy the trigger.
 

ZakkWylde-

New Member
Reaction score
14
call TriggerRegisterUnitInRange(gg_trg_Kill, spinUnit[index], uC, null)

>> This is going to leak until you destroy the trigger.

Currently, every time any new set of units are spawned, I destroy the trigger and reinitialize it.
Will this prevent the leaking?
 

huanAk

New Member
Reaction score
4
Currently, every time any new set of units are spawned, I destroy the trigger and reinitialize it.
Will this prevent the leaking?

you can't destroy and reinitialize it. unless you mean :

destroy trigger and re-create it (call MakeSpinUnit will automatic create it)
disable trigger and reinitialize it <-- leak, because MakeSpinUnit will automatic create it again
 

ZakkWylde-

New Member
Reaction score
14
This is what I meant xD. Does this work/reduce lag/remove leaks?

JASS:
    call ForGroup(udg_EnemyUnits, function RemoveUnits)
    call DestroyTrigger(gg_trg_Kill)
    call InitTrig_Kill()

    ................ //InitTrig_Kill() and relevant things below  (they are actually in the map header)

function KillCondition takes nothing returns boolean
    local integer Id = GetPlayerId( GetOwningPlayer( GetTriggerUnit() ) )
    return (Id != 12 and Id != 15) and (not isUnitInvuln[Id])
endfunction

function Trig_Kill_Actions takes nothing returns nothing
    call KillUnit(GetTriggerUnit())
endfunction

function InitTrig_Kill takes nothing returns nothing
    set gg_trg_Kill = CreateTrigger(  )
    call TriggerAddCondition(gg_trg_Kill, Condition(function KillCondition))
    call TriggerAddAction( gg_trg_Kill, function Trig_Kill_Actions )
endfunction
 

ZakkWylde-

New Member
Reaction score
14
XD Dirac, thank you for posting improved code. :D How much faster is this actually?
Now, does [ljass]unit u[/ljass] leak when you call the function as a Condition?

Also, is this the best way to do it if I'm adding the line
[ljass]call TriggerRegisterUnitInRange(gg_trg_Kill, unitCreated, 60, null) //gg_trg_Kill was the name of the trigger--now what?[/ljass]
every time I create an enemy unit? I'm really not too clear on: to which trigger do I add that "registerunitinrange" if it wasn't actually named when created?
 

huanAk

New Member
Reaction score
4
XD Dirac, thank you for posting improved code. :D How much faster is this actually?
Now, does [ljass]unit u[/ljass] leak when you call the function as a Condition?

Also, is this the best way to do it if I'm adding the line
[ljass]call TriggerRegisterUnitInRange(gg_trg_Kill, unitCreated, 60, null) //gg_trg_Kill was the name of the trigger--now what?[/ljass]
every time I create an enemy unit? I'm really not too clear on: to which trigger do I add that "registerunitinrange" if it wasn't actually named when created?

unit u do not leak. it will leak if you write them in this way :

local unit u
set u = GetTriggerUnit()
(now, you have to set u to null at the end)

I am a bit lost on 2nd part :
- if you only call MakeSpinUnit once, then you don't need destory trigger, because it is it register with TriggerRegisterUnitInRange(gg_trg_Kill, unitCreated, 60, null)

- if you call MakeSpinUnit more than 1 time, then you need destory trigger before call it again.

and never destory trigger if it have "TriggerRegisterBlahblah" (unless you don't need that trigger anymore)
 

ZakkWylde-

New Member
Reaction score
14
What exactly is the difference between:
[ljass]local unit u = GetTriggerUnit()[/ljass]
and
[ljass]local unit u ... set u = GetTriggerUnit()[/ljass]?

Also, the reason I destroy the trigger (and then remake it) is so that it doesn't have all those unnecessary [ljass]TriggerRegisterUnitInRange[/ljass] events tied to it, when those units aren't even in the game anymore. And yes, I call MakeSpinUnit more than once.
 

Laiev

Hey Listen!!
Reaction score
188
[ljass]local unit u = GetTriggerUnit()[/ljass]
this is more common and more intelligent

[ljass]local unit u
set u = GetTriggerUnit()[/ljass]
this is stupid, and only viable inside some loop that you'll check something before set variables
 

huanAk

New Member
Reaction score
4
local unit u = GetTriggerUnit()
-- you only allocate 1 "block" of memory to this variable

local unit u
set u = GetTriggerUnit()
-- you allocate 1 "block" in first line.
-- then 2nd "block' from 2nd line

so you need to call "set u =null" to clear 2nd block and move back to original. because blizzard can only remove current memory "block" and all other "block" become lost and stay there forever, hence it call leaked
 

tooltiperror

Super Moderator
Reaction score
231
>then 2nd "block' from 2nd line
Why would you allocate any more memory from using doing a local declaration and a set than both in one line? They are both performing the same exact operation, the only difference would probably be parsing speed.
 

ZakkWylde-

New Member
Reaction score
14
>then 2nd "block' from 2nd line
Why would you allocate any more memory from using doing a local declaration and a set than both in one line? They are both performing the same exact operation, the only difference would probably be parsing speed.

This is what I was thinking...
which led me to the question of why one leaked and the other did not...
Still not satisfied--I somehow feel that both will leak, whatever people who know more than me may tell me xD
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Members online

      No members online now.

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top