• 🏆 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!
  • 🏆 Hive's 6th HD Modeling Contest: Mechanical is now open! Design and model a mechanical creature, mechanized animal, a futuristic robotic being, or anything else your imagination can tinker with! 📅 Submissions close on June 30, 2024. Don't miss this opportunity to let your creativity shine! Enter now and show us your mechanical masterpiece! 🔗 Click here to enter!

Is this leaking??

Status
Not open for further replies.
Level 7
Joined
Dec 30, 2008
Messages
72
Hi, I am very new to WC3 programming, although not new to programming, and am having a little trouble understanding what leaks and what doesn't... I have read through some tutes on leaking and I think I may understand it but would appreciate it if someone can tell me whether this code is leaking in any form, and what else could I clean up.

JASS:
function Trig_Steal_Actions takes nothing returns nothing
    //Local variables
    local player TargetPlayer
    local player ThiefPlayer
    local integer Gold
    local integer Lumber
    local texttag PGold
    local texttag PLumber
    local unit TargetUnit
    local location TargetUnitPos

    //Set variables
    set TargetPlayer = GetOwningPlayer(GetSpellTargetUnit())
    set ThiefPlayer = GetOwningPlayer(GetSpellAbilityUnit())
    set TargetUnit = GetSpellTargetUnit()
    set TargetUnitPos = GetUnitLoc(TargetUnit)
    set Lumber = ( ( GetPlayerState(TargetPlayer, PLAYER_STATE_RESOURCE_LUMBER) * 10 ) / 100 )
    set Gold = ( ( GetPlayerState(TargetPlayer, PLAYER_STATE_RESOURCE_GOLD) * 10 ) / 100 )
    
    //Begin ability: Remove casting unit and adjust gold and lumber
    call RemoveUnit( GetSpellAbilityUnit() )
    call AdjustPlayerStateBJ( Gold, ThiefPlayer, PLAYER_STATE_RESOURCE_GOLD )
    call AdjustPlayerStateBJ( Lumber, ThiefPlayer, PLAYER_STATE_RESOURCE_LUMBER )
    call AdjustPlayerStateBJ( ( Gold * -1 ), TargetPlayer, PLAYER_STATE_RESOURCE_GOLD )
    call AdjustPlayerStateBJ( ( Lumber * -1 ), TargetPlayer, PLAYER_STATE_RESOURCE_LUMBER )

    //Create Gold text
    call CreateTextTagLocBJ( ( "Gold: -" + I2S(Gold) ), TargetUnitPos, 0, 10, 100, 0.00, 0.00, 0 )
    set PGold = GetLastCreatedTextTag()
    call SetTextTagPermanentBJ( PGold, false )
    call SetTextTagLifespanBJ( PGold, 3.00 )
    call SetTextTagFadepointBJ( PGold, 2.00 )
    call SetTextTagVelocityBJ( PGold, 64, 90 )

    //Create Lumber text
    call CreateTextTagLocBJ( ( "Lumber: -" + I2S(Lumber) ), TargetUnitPos, -50, 10, 100, 0.00, 0.00, 0 )
    set PLumber = GetLastCreatedTextTag()
    call SetTextTagPermanentBJ( PLumber, false )
    call SetTextTagLifespanBJ( PLumber, 3.00 )
    call SetTextTagFadepointBJ( PLumber, 2.00 )
    call SetTextTagVelocityBJ( PLumber, 64, 90 )

    //Clean up location
    call RemoveLocation(TargetUnitPos)
endfunction
 
Level 13
Joined
Mar 16, 2008
Messages
941
Only a little bit. You've removed the location correctly, but you need to "null" handles. What I'm talking about is this:
JASS:
...

    //Clean up location
    set TargetUnit = null
    call RemoveLocation(TargetUnitPos)
    set TargetUnitPos = null //noooo,  I didn't forget this one ^^''
endfunction

This would remove all "leaks". Not nulled handles doesn't leak, but they can cause problems which also result in slowing down the computer, like leaks do :p
In addition, try to make it a bit more performant:
- Use a variable for "GetSpellAbilityUnit" (you could use GetTriggerUnit, but that's not that important)
- Try to avoid "BJs" that are useless:
JASS:
//This is a useless BJ. You can see that it's only a wrapper function, so it "costs" a call more but is worth nothing
function SetTextTagPermanentBJ takes texttag tt, boolean flag returns nothing
    call SetTextTagPermanent(tt, flag)
endfunction
//This BJ is ok (for the start, you should avoid most BJs later on), since it's more complex and makes it easier for you
function SetTextTagVelocityBJ takes texttag tt, real speed, real angle returns nothing
    local real vel = TextTagSpeed2Velocity(speed)
    local real xvel = vel * Cos(angle * bj_DEGTORAD)
    local real yvel = vel * Sin(angle * bj_DEGTORAD)

    call SetTextTagVelocity(tt, xvel, yvel)
endfunction

If you've problems finding out the names of BJ-removal functions, use the JNGP or JassCraft native list :)
 
Last edited:
Status
Not open for further replies.
Top