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

[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