HomeUser Control Panel (unavailable in archive)ForumsTutorialsArt GalleryResourcesMaps

Memory leak problems

05-17-2009, 01:34 PM#1
bastanien
Hi i just started to use jass yesterday and im having some problems with memory leaks in my triggers, plz help :o

Collapse JASS:
function Trig_Cast_Actions takes nothing returns nothing

local real x
local real y
local integer r
local integer g
local integer b
local integer spell
local texttag text

if(udg_CustomValue < 100) then
set udg_CustomValue = udg_CustomValue+1
else
set udg_CustomValue=1
endif

set udg_Caster[udg_CustomValue] = GetTriggerUnit()
set udg_Distance[udg_CustomValue] = 200
set udg_TargetAngle[udg_CustomValue] =  AngleBetweenPoints(GetUnitLoc(udg_Caster[udg_CustomValue]),GetSpellTargetLoc()  )

    if (GetAbilityName(GetSpellAbilityId())=="FireBolt") then // color floating text red when casting firebolt
    set r = 255 
    set g = 0 
    set b = 0
    set spell = 'n001'
    elseif(GetAbilityName(GetSpellAbilityId())=="FrostBolt") then // color floating text blue when casting frostbolt
    set r = 0 
    set g = 0 
    set b = 255
    set spell = 'n000'
    elseif(GetAbilityName(GetSpellAbilityId())=="Cannonball") then  // color floating text brown when casting Missile
    set r = 140
    set g = 120 
    set b = 100
    set spell = 'n002'
    endif    

    set x = GetUnitX(GetTriggerUnit())
    set y = GetUnitY(GetTriggerUnit())
   
    set udg_SpellUnit[udg_CustomValue] = CreateUnit(GetOwningPlayer(GetTriggerUnit()), spell , x , y, udg_TargetAngle[udg_CustomValue])  // create the spellunit    
    call GroupAddUnit(udg_SpellGroup, udg_SpellUnit[udg_CustomValue])    // add unit to spellgroup for Move Trigger
    call SetUnitUserData(udg_SpellUnit[udg_CustomValue], udg_CustomValue )  // change custom value of unit for MUI	
    
        set text = CreateTextTag()
        call SetTextTagText(text,GetAbilityName(GetSpellAbilityId()),0.020)          // show what spell is casted by casting unit   
        call SetTextTagPos(text,x,y,10)
        call SetTextTagColor(text,r,g,b,255)           
        call SetTextTagVelocity(text,0.025,0.05)
        call SetTextTagPermanent(text, false)
        call SetTextTagLifespan(text,1.5)
        call SetTextTagFadepoint(text,1)       
        set text = null
        
endfunction

//===========================================================================
function InitTrig_Cast takes nothing returns nothing
    
    set gg_trg_Cast = CreateTrigger(  )
    call TriggerAddAction( gg_trg_Cast, function Trig_Cast_Actions )
    call TriggerRegisterAnyUnitEventBJ(gg_trg_Cast,EVENT_PLAYER_UNIT_SPELL_EFFECT)  // any unit casts a spell
    
endfunction

Collapse JASS:
function Trig_InitTrig_Move_Conditions takes nothing returns boolean
    return CountUnitsInGroup(udg_SpellGroup) > 0
endfunction

function PickedUnits takes nothing returns nothing
    local real x
    local real y 
    
    set udg_UnitValue = GetUnitUserData(GetEnumUnit())
    if(udg_Distance[udg_UnitValue] > 0) then
        set udg_Distance[udg_UnitValue] = udg_Distance[udg_UnitValue]-5
        set x = GetUnitX(udg_SpellUnit[udg_UnitValue]) + 30 * Cos(udg_TargetAngle[udg_UnitValue] * bj_DEGTORAD)
        set y = GetUnitY(udg_SpellUnit[udg_UnitValue]) + 30 * Sin(udg_TargetAngle[udg_UnitValue] * bj_DEGTORAD)
        call SetUnitX(udg_SpellUnit[udg_UnitValue], x)
        call SetUnitY(udg_SpellUnit[udg_UnitValue], y)
    else
        call KillUnit(udg_SpellUnit[udg_UnitValue])
        call PolledWait(0.1)
        call RemoveUnit(udg_SpellUnit[udg_UnitValue])
    endif
endfunction

function Trig_Move_Actions takes nothing returns nothing
    call ForGroup(udg_SpellGroup, function PickedUnits)
endfunction

//===========================================================================
function InitTrig_Move takes nothing returns nothing
    local trigger t = CreateTrigger()
    call TriggerRegisterTimerEvent(t, 0.035, true)
    call TriggerAddCondition(t, Condition(function Trig_InitTrig_Move_Conditions))
    call TriggerAddAction(t, function Trig_Move_Actions)
  endfunction


anyone got any idea of what might be wrong?

Edit:

sry for double post, didn't know about the moderation thingy.

and i managed to fix most of the memory leak problems.
05-17-2009, 10:46 PM#2
Silvenon
You have a leak here:

set udg_TargetAngle[udg_CustomValue] = AngleBetweenPoints(GetUnitLoc(udg_Caster[udg_CustomValue]),GetSpellTargetLoc() )

You aren't destroying the two locations generated by GetUnitLoc and GetSpellTargetLoc. You can fix it by putting both locations in variables and destroy them later with RemoveLocation:

Collapse JASS:
local location l1 = GetUnitLoc(udg_Caster[udg_CustomValue])
local location l2 = GetSpellTargetLoc()
// ...
set udg_TargetAngle[udg_CustomValue] =  AngleBetweenPoints(l1, l2)
// ...
call RemoveLocation(l1)
call RemoveLocation(l2)

Also, you have a really small leak here:

Collapse JASS:
function InitTrig_Move takes nothing returns nothing
    local trigger t = CreateTrigger()
    call TriggerRegisterTimerEvent(t, 0.035, true)
    call TriggerAddCondition(t, Condition(function Trig_InitTrig_Move_Conditions))
    call TriggerAddAction(t, function Trig_Move_Actions)
  endfunction

But because this function is called only once and because leaks caused by not nullifying are really small anyways, this is not a problem.
05-18-2009, 07:56 PM#3
busterkomo
Local triggers don't need to be nulled, as they are generally existent throughout the entire game.
05-19-2009, 06:21 AM#4
0zyx0
You also have to null the location variables.
05-19-2009, 09:22 AM#5
Silvenon
Quote:
Originally Posted by 0zyx0
You also have to null the location variables.

Whoops, yeah :D

Collapse JASS:
local location l1 = GetUnitLoc(udg_Caster[udg_CustomValue])
local location l2 = GetSpellTargetLoc()
// ...
set udg_TargetAngle[udg_CustomValue] =  AngleBetweenPoints(l1, l2)
// ...
call RemoveLocation(l1)
call RemoveLocation(l2)
set l1 = null
set l2 = null

Quote:
Local triggers don't need to be nulled, as they are generally existent throughout the entire game.

They don't have to be nulled because they are created only once in the game.