• 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.

[Snippet] LocationUtils

Level 7
Joined
Apr 27, 2011
Messages
272
Just a simple implementation of a location stack to help prevent location leaks.

JASS:
library LocationUtils
//===========================================================================
//	"LocationUtils" by Alain.Mark
//
//	-Info-
//	 -Attempts to remove the problem of location leaks by providing a location stack.
//
//	-API-
//	 -function NewLocationEx takes real x, real y location
//	 -function NewLocation takes nothing returns location
//	 -function DestroyLocation takes location l returns nothing
//
//===========================================================================
	globals
		private integer N=0
		private location array Ll
	endglobals
	
//===========================================================================
	function NewLocationEx takes real x, real y returns location
		if(N==0)then
			return Location(x,y)
		endif
		set N=N-1
		call MoveLocation(Ll[N],x,y)
		return Ll[N]
	endfunction
	
//===========================================================================
	function NewLocation    takes nothing returns location
		return NewLocationEx(0.00,0.00)
	endfunction
	
//===========================================================================
	function DestroyLocation takes location l returns nothing
		set Ll[N]=l
		set N=N+1
	endfunction
	
//===========================================================================
endlibrary
 
Recycling is mainly useful for:

Units (because creating them takes a lot of CPU)
Timers (because back in the day when TimerUtils used handle ID offsets, you wanted to preload them before the handle stack got too high, and destroying a timer without pausing it can cause it to still expire)
Groups (because once enumerating, there will be some small memory leaked when destroyed)
 
Level 26
Joined
Aug 18, 2009
Messages
4,097
Yeah, there is no need to have such a library. The concept is not new, location is a basic object. You do not need to create locations dynamically besides GetUnitRallyPoint maybe, which does not work here. It's not even always the best option to cache everything, creation and destruction of locations is cheap too. Only if you really want to use locations for whatever reason can this help to be safe of handle leaks.
 
Top