Leaks

Builder Bob

Live free or don't
Reaction score
249
SFilip, could you please help me understand what really happens to the boolexpr in the following trigger?
I'm assuming local boolexpr f = Condition(function KillSelected) creates a handle and makes f point to it.
I'm also assuming DestroyBoolExpr(f) destroys the handle f is pointing to, and not the actual condition.
Why else, would this trigger work?
I'm pretty sure I'm wrong on something here, I just don't know what.


The trigger kills every selected unit on esc.
JASS:
function KillSelected takes nothing returns boolean
	call KillUnit(GetFilterUnit())
	return false
endfunction

function OnEsc takes nothing returns boolean
	local group g = CreateGroup()
	local boolexpr f = Condition(function KillSelected)
	call GroupEnumUnitsSelected(g, GetTriggerPlayer(), f)
	call DestroyBoolExpr(f)
	call DestroyGroup(g)
	set f = null
	set g = null
	return false
endfunction

//===========================================================================
function InitTrig_SelectedOnEsc takes nothing returns nothing
	local trigger trigEsc = CreateTrigger()
	local integer i = 0
	loop
		call TriggerRegisterPlayerEvent(trigEsc, Player(i), EVENT_PLAYER_END_CINEMATIC)
		set i = i + 1
		exitwhen(i == 12)
	endloop
	call TriggerAddCondition(trigEsc, Condition(function OnEsc))
	set trigEsc = null
endfunction



kallieblakie, I'll look through your code and see what I can find

Edit: I can't compile this for obvious reasons, but I think it is leakfree now.

I took the liberty to remove some locations in favor of coordinates. It's easier to work with as reals and integers can't leak. You can change the rest if you want to. Variables usually only have to be destroyed and nulled at the same indentation level that they are created. This is by no means a rule though.

JASS:
function Arrow_Filter takes nothing returns boolean
	return IsUnitType(GetFilterUnit(), UNIT_TYPE_STRUCTURE) != true and GetWidgetLife(GetFilterUnit()) > 0.405 
endfunction

function Trig_Arrow_Elunes_Arrow_Conditions takes nothing returns boolean
	return GetSpellAbilityId() == 'AOsh'
endfunction

function DPP takes location locA, location locB returns real
	local real dx = GetLocationX(locB) - GetLocationX(locA)
	local real dy = GetLocationY(locB) - GetLocationY(locA)
	return SquareRoot(dx * dx + dy * dy)
endfunction

function Arrow_Move takes nothing returns nothing
	local timer Loop = GetExpiredTimer()
	local unit arrow = GetHandleUnit(Loop, "arrow")
	local unit caster = GetHandleUnit(Loop, "caster")
	local real angle = GetHandleReal(Loop, "angle")
	local integer Range = GetHandleInt(Loop, "Range")
	local integer Radius = GetHandleInt(Loop, "Radius")
	local real Speed = GetHandleReal(Loop, "Speed")
	local integer PlayerNo = GetHandleInt(Loop, "PlayerNo")
	local real distanceCurrent = GetHandleReal(Loop, "distanceCurrent")
	local location C
	local location Next
	local boolexpr b
	local real x 
	local real y
	local group g
	local unit p
	local unit dummy
	local integer Number
	local location locA
	local location locB
	if (R2I(distanceCurrent) <= Range) then
		set distanceCurrent = distanceCurrent + Speed
		call SetHandleReal (Loop, "distanceCurrent", distanceCurrent)
		set x = GetUnitX(arrow) + Speed * Cos(angle * bj_DEGTORAD)
		set y = GetUnitY(arrow) + Speed * Sin(angle * bj_DEGTORAD)
		call SetUnitPosition(arrow, x, y)
		set g = CreateGroup()
		set b = Condition(function Arrow_Filter)
		call GroupEnumUnitsInRangeCounted(g, x, y, I2R(Radius), b, 1)
		call DestroyBoolExpr(b)
		set b = null
		set p = FirstOfGroup(g)
		call DestroyGroup(g)
		set g = null
		if p != null then
			if ( ( UnitHasBuffBJ(p, 'BPSE') == true ) ) then
				call UnitDamageTarget(caster, p, 100000, false, false, ATTACK_TYPE_NORMAL, DAMAGE_TYPE_NORMAL, null)
				call RemoveUnit(arrow)
				
				//---I think you should flush and destroy the timer here as well
				
				call FlushHandleLocals(Loop)
				call PauseTimer(Loop)
				call DestroyTimer(Loop)
			elseif ( (IsUnitEnemy(p, GetOwningPlayer(caster)) == true)) then
				call UnitDamageTarget(caster, p, udg_Damage[PlayerNo], false, false, ATTACK_TYPE_NORMAL, DAMAGE_TYPE_NORMAL, null)

				set dummy = CreateUnitAtLoc(GetOwningPlayer(caster), 'hfoo', Next, 0.00)
				call UnitAddAbility(dummy, 'ANsb')
				call SetUnitAbilityLevel(dummy, 'ANsb', udg_Stun[PlayerNo])
				call IssueTargetOrder(dummy, "thunderbolt", p)
				call UnitApplyTimedLife(dummy, 'BTLF', 2.00)

				set locA = GetUnitLoc(caster)
				set locB = GetUnitLoc(arrow)
				call UnitDamageTarget(caster, p, (( I2R(udg_I_PrecisionLevel[PlayerNo]) * ( 0.06 * DPP(locA,locB)))+( ( 35000.00 * ( 0.40 * I2R(udg_I_bladeLevel[PlayerNo]) ) ) / DPP(locA,locB))),false,true, ATTACK_TYPE_NORMAL, DAMAGE_TYPE_NORMAL, WEAPON_TYPE_METAL_HEAVY_BASH )
				call RemoveLocation (locA)
				call RemoveLocation (locB)
				set locA = null
				set locB = null
				call RemoveUnit(arrow)
				call FlushHandleLocals(Loop)
				call PauseTimer(Loop)
				call DestroyTimer(Loop)
			endif
			set p = null
		endif
	elseif (R2I(distanceCurrent) > Range) then
		call KillUnit(arrow)
		call RemoveUnit(arrow)
		call FlushHandleLocals(Loop)
		call PauseTimer(Loop) 
		call DestroyTimer(Loop) 
	endif
	set arrow = null
	set caster = null
	set Loop = null
endfunction

function Trig_Arrow_Elunes_Arrow_Actions takes nothing returns nothing
	local timer Loop = CreateTimer()
	local unit caster = GetSpellAbilityUnit()
	local location casterPosition = GetUnitLoc(caster)
	local location targetPosition = GetSpellTargetLoc()
	local integer PlayerNo = GetConvertedPlayerId(GetOwningPlayer(GetSpellAbilityUnit())) //I won't start messing with playerIds, but I like Player() and GetPlayerId() functions better
	local integer radius = udg_Radius[PlayerNo]
	local real angle = AngleBetweenPoints(casterPosition, targetPosition)
	local real x = GetLocationX(casterPosition) + (radius+2)*Cos(angle*bj_DEGTORAD)
	local real y = GetLocationY(casterPosition) + (radius+2)*Sin(angle*bj_DEGTORAD)
	local real Scale = ( 100.00 * ( I2R(radius) / 80.00 ) )
	
	local integer Range = udg_Range[PlayerNo]
	local real Speed = udg_Speed[PlayerNo]
	local unit arrow = CreateUnit(ConvertedPlayer(PlayerNo), 'hpea', x, y, angle)
	call SetUnitScalePercent( arrow, Scale, Scale, Scale )
	call SetUnitPathing( arrow, false )
	call RemoveLocation(casterPosition)
	call RemoveLocation(targetPosition)
	
	call SetHandleHandle(Loop, "arrow", arrow)
	call SetHandleHandle(Loop, "caster", caster)
	call SetHandleReal(Loop,"angle",angle)
	call SetHandleInt(Loop, "Range", Range)
	call SetHandleReal(Loop, "Speed", Speed)
	call SetHandleInt(Loop, "Radius", radius)
	call SetHandleInt(Loop, "PlayerNo", PlayerNo)
	call TimerStart(Loop, 0.03, true, function Arrow_Move)
	set Loop = null
	set caster = null
	set casterPosition = null
	set targetPosition = null
	set arrow = null
endfunction

//===========================================================================
function InitTrig_Arrow_Elunes_Arrow takes nothing returns nothing
	set gg_trg_Arrow_Elunes_Arrow = CreateTrigger()
	call TriggerRegisterAnyUnitEventBJ( gg_trg_Arrow_Elunes_Arrow, EVENT_PLAYER_UNIT_SPELL_EFFECT )
	call TriggerAddCondition( gg_trg_Arrow_Elunes_Arrow, Condition( function Trig_Arrow_Elunes_Arrow_Conditions ) )
	call TriggerAddAction( gg_trg_Arrow_Elunes_Arrow, function Trig_Arrow_Elunes_Arrow_Actions )
endfunction
 

Builder Bob

Live free or don't
Reaction score
249
Anyway, the trigger doesn't work! it doesn't properly destroy the loop when it hits a unit. ARGGHS

Hmm... that's weird. I must have done a mistake. Can you tell me more about what happens?

Does the arrow move as it should and as far as it should?
Do units get damaged when the arrow goes through them?
Does it hit some times but not always?
etc.

I need more info to correct the mistake without being able to test myself.


Here is how you can pinpoint the bug yourself:
Place small debug messages after every if, elseif and else so you know what code is being run and what's not.

Then find out why the if/elseif argument isn't returning true by checking what the varibales in that argument is all the way from the beginning of the trigger and up to the if/elseif in question. When you find out where the value is wrong, you can usually know why and correct it.


Addition - Skip this if you want:

Put this function in your header. If you don't know what that is, look for mapname.w3x above all your triggers.
JASS:
function DebugUnitTag takes string s, unit u returns nothing
	local texttag tag = CreateTextTag()
	local integer width = StringLength(s)
	call SetTextTagText(tag, s, 0.023)
	call SetTextTagPos(tag, GetUnitX(u) - width * 10, GetUnitY(u), 0.)
	call SetTextTagColor(tag, 255, 50, 50, 255)
	call SetTextTagPermanent(tag, false)
	call SetTextTagLifespan(tag, 3.)
	call SetTextTagFadepoint(tag, 1.5)
	call SetTextTagVelocity(tag, 0., .071)
	set tag = null
endfunction


Now you can write DebugUnitTag("something or whatever", p) and you will get a floating text saying "something or whatever" over the unit p. It's leakless and I find it can help pinpoint bugs much easier.

Good hunting!
 

kallieblakie

New Member
Reaction score
5
Hmm... that's weird. I must have done a mistake. Can you tell me more about what happens?

Does the arrow move as it should and as far as it should?
Do units get damaged when the arrow goes through them?
Does it hit some times but not always?
etc.

I need more info to correct the mistake without being able to test myself.


Here is how you can pinpoint the bug yourself:
Place small debug messages after every if, elseif and else so you know what code is being run and what's not.

Then find out why the if/elseif argument isn't returning true by checking what the varibales in that argument is all the way from the beginning of the trigger and up to the if/elseif in question. When you find out where the value is wrong, you can usually know why and correct it.
I've already corrected the mistake. The script you made allows the arrow to does extra few hits to the enemy before it disappears. say, supposingly it should deal 100 damage, your script allows the arrow to "poke" the enemy 4-5 times, dealing 500 damage instead.


JASS:
			elseif ( (IsUnitEnemy(p, GetOwningPlayer(caster)) == true)) then
				call UnitDamageTarget(caster, p, udg_Damage[PlayerNo], false, false, ATTACK_TYPE_NORMAL, DAMAGE_TYPE_NORMAL, null)

				set dummy = CreateUnitAtLoc(GetOwningPlayer(caster), 'hfoo', Next, 0.00)
				call UnitAddAbility(dummy, 'ANsb')
				call SetUnitAbilityLevel(dummy, 'ANsb', udg_Stun[PlayerNo])
				call IssueTargetOrder(dummy, "thunderbolt", p)
				call UnitApplyTimedLife(dummy, 'BTLF', 2.00)

				set locA = GetUnitLoc(caster)
				set locB = GetUnitLoc(arrow)
				call UnitDamageTarget(caster, p, (( I2R(udg_I_PrecisionLevel[PlayerNo]) * ( 0.06 * DPP(locA,locB)))+( ( 35000.00 * ( 0.40 * I2R(udg_I_bladeLevel[PlayerNo]) ) ) / DPP(locA,locB))),false,true, ATTACK_TYPE_NORMAL, DAMAGE_TYPE_NORMAL, WEAPON_TYPE_METAL_HEAVY_BASH )
				call RemoveLocation (locA)
				call RemoveLocation (locB)
				set locA = null
				set locB = null
				call RemoveUnit(arrow)
				call FlushHandleLocals(Loop)
				call PauseTimer(Loop)
				call DestroyTimer(Loop)


the
JASS:
				call RemoveUnit(arrow)

cannot be placed so low in the trigger. I shifted it up and it works perfectly fine.

Thanks for your help!

my map is now available at www.geocities.com/kallieblakie
 

Builder Bob

Live free or don't
Reaction score
249
Ah, very nice that you fixed it. Does it leak anymore?

I have an ability much like this, and I remove the arrow at the very end of the code. Guess yours does something I don't really understand or something.

By the way... Now that I look over the code I posted for you again, I see some mistakes. I removed the Next location from most of the code and replaced it with x and y, but I didn't remember to remove it where the dummy is created.

In any case, glad to be of some help.
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Staff online

      Members online

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top