• 🏆 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!
  • It's time for the first HD Modeling Contest of 2024. Join the theme discussion for Hive's HD Modeling Contest #6! Click here to post your idea!

[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
 

Bribe

Code Moderator
Level 50
Joined
Sep 26, 2009
Messages
9,464
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