• Listen to a special audio message from Bill Roper to the Hive Workshop community (Bill is a former Vice President of Blizzard Entertainment, Producer, Designer, Musician, Voice Actor) 🔗Click here to hear his message!
  • Read Evilhog's interview with Gregory Alper, the original composer of the music for WarCraft: Orcs & Humans 🔗Click here to read the full interview.

Small Question Regarding Leaks

Status
Not open for further replies.
Level 19
Joined
Oct 29, 2007
Messages
1,184
JASS:
set l = Location(x, y)
set l = Location(y, x)
call RemoveLocation(l)

Will l leak?

I always do it like:

JASS:
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
 
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.
 
Level 12
Joined
Jul 27, 2008
Messages
1,181
Best explanation possible is a small explanation of pointers.

If you have:
JASS:
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).
 
Level 22
Joined
Dec 31, 2006
Messages
2,216
When you have local variables you must null them if they are handles with the exception of players. And the second one is right.
JASS:
local location l
set l = Location(0., 0.)
call RemoveLocation(l)
set l = Location(0., 0.)
call RemoveLocation(l)
set l = null
 
Status
Not open for further replies.
Top