Wc3C.net (http://www.wc3c.net/forums.php)
-   General Development (http://www.wc3c.net/forumdisplay.php?f=189)
-   -   Does the use of this function DistanceBetweenPoints cause memery leak? (http://www.wc3c.net/showthread.php?t=110949)

 yeeboys 04-20-2017 04:17 AM

Does the use of this function DistanceBetweenPoints cause memery leak?

this is a function in BJ.

JASS:
```function DistanceBetweenPoints 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```

this is a Custom function.

JASS:
```function func1 takes nothing returns nothing
local location A = GetUnitLoc(unit_a)
local location B = GetUnitLoc(unit_b)
local real dist = DistanceBetweenPoints(A,B)
call RemoveLocation(A)
call RemoveLocation(B)
set A = null
set B = null
endfunction```

Does the use of this function DistanceBetweenPoints cause memery leak? Local location A and B was removed at func1. But location locA and locB wasn't removed at DistanceBetweenPoints.

 Fledermaus 04-20-2017 07:16 AM

1 Attachment(s)
No it doesn't leak because they're parameters/arguments. You only need to destroy/null local/global variables (globals don't have to be nulled because you can't ever lose the reference to that variable).
Attached a testmap so you can play around and see for yourself :)

However most people don't like using locations because they can leak.
Here are 2 commonly used alternatives:
JASS:
```function DistanceBetweenXY takes real x1, real y1, real x2, real y2 returns real
return SquareRoot(((x2 - x1) * (x2 - x1)) + ((y2 - y1) * (y2 - y1)))
endfunction

function DistanceBetweenUnits takes unit a, unit b returns real
return SquareRoot(((GetUnitX(b) - GetUnitX(a)) * (GetUnitX(b) - GetUnitX(a))) + ((GetUnitY(b) - GetUnitY(a)) * (GetUnitY(b) - GetUnitY(a))))
endfunction
```

 All times are GMT. The time now is 02:07 AM.