# Small Question Regarding Leaks

Discussion in 'Triggers & Scripts' started by Megafyr, Oct 5, 2009.

1. ### Megafyr

Joined:
Oct 29, 2007
Messages:
957
Resources:
1
Tutorials:
1
Resources:
1
Code (vJASS):
set l = Location(x, y)
set l = Location(y, x)
call RemoveLocation(l)

Will l leak?

I always do it like:

Code (vJASS):
set l = Location(x, y)
call RemoveLocation(l)
set l = Location(y, x)
call RemoveLocation(l)

But it seems like I am doing more than I have to. right? xD

2. ### Reaper2008

Joined:
Jul 27, 2008
Messages:
1,117
Resources:
1
Spells:
1
Resources:
1
Second one is right.

3. ### Super-Sheep

Joined:
Oct 21, 2006
Messages:
2,823
Resources:
1
Maps:
1
Resources:
1
I have asked this same question and I was told that first one was right. -.-

not sure thought that did I understand what you mean, cause of y,x stuff.

4. ### Diablo-dk

Joined:
Nov 10, 2004
Messages:
369
Resources:
3
Maps:
1
Spells:
1
Tutorials:
1
Resources:
3
The second example is the right way to do it.

An easy way to understand why, is to think of locations as objects. Each time you do a Location(x, y) call, you create a new object that has to be removed later, and since you are creating 2 locations but only removing one of them in your first example, it will leak.

By the way, it is more efficient to use a single global location instead of local locations, since you always have to create and destroy them, whereas you can just do a MoveLocation() call with a global location.

5. ### Reaper2008

Joined:
Jul 27, 2008
Messages:
1,117
Resources:
1
Spells:
1
Resources:
1
Best explanation possible is a small explanation of pointers.

If you have:
Code (vJASS):
local location loc
set loc = Location (0, 0)
set loc = Location (1, 1)
call Remove Location(loc)

The variable points to the location, it isn't the location. And if you change it the location it pointed to before remains, but it is unreachable, and eats up memory (= leak).

Joined:
Oct 29, 2007
Messages:
957
Resources:
1
Tutorials:
1
Resources:
1

7. ### Saia_Djinn

Joined:
Feb 15, 2009
Messages:
458
Resources:
0
Resources:
0
but you should also
Code (vJASS):
null
the variable or it will still leak a lil bit after removing

8. ### The Reborn Devil

Joined:
Dec 31, 2006
Messages:
1,325
Resources:
0
Resources:
0
When you have local variables you must null them if they are handles with the exception of players. And the second one is right.
Code (vJASS):

local location l
set l = Location(0., 0.)
call RemoveLocation(l)
set l = Location(0., 0.)
call RemoveLocation(l)
set l = null

9. ### Megafyr

Joined:
Oct 29, 2007
Messages:
957
Resources:
1
Tutorials:
1
Resources:
1
I always do that.