• 🏆 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!

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
 
Level 10
Joined
Nov 10, 2004
Messages
351
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