1. The poll for our 11th Music Contest is up! Help us choose the most awesome cinematic tracks by casting a vote!
    Dismiss Notice
  2. Melee Mapping contest #3 - Poll is up! Vote for the best 4v4 melee maps!
    Dismiss Notice
  3. The 30th edition of the Modeling Contest is finally up! The Portable Buildings need your attention, so come along and have a blast!
    Dismiss Notice
  4. The Aftermath has been revealed for the 19th Terraining Contest! Be sure to check out the Results and see what came out of it.
    Dismiss Notice

Basic Memory Leaks

Discussion in 'Trigger (GUI) Editor Tutorials' started by Ralle, Jan 3, 2006.

  1. Ralle

    Ralle

    Owner

    Joined:
    Oct 6, 2004
    Messages:
    11,117
    Resources:
    22
    Tools:
    3
    Maps:
    5
    Tutorials:
    14
    Resources:
    22
    by thunder_eye...

    Lets start with a brief overview of how leaks happen, or more precisely, how handle object leaks happen. These memory leaks are what we deal with the most, there are others, like string leaks (which can't be prevented at all, but are usually insignificant), and the problem with local handle variables (which is a part of handle object leaks but won't be covered here for the sake of keeping it short and simple, because local variables are only used in JASS anyway). By the way, custom scripts are JASS.

    Handles are game objects... units, doodads, destructibles, items, special effects and also regions, locations, unit groups, player groups... all this stuff. Things that are not handles are integers, reals, booleans; these are just numbers. They are stored in their variables and are erased the moment the variable is set to a new value.

    Handle variables, on the other hand, are pointers: they only contain a number that points to where in the game's memory the object is located. Handle objects exist independent of variables. They can exist without any variables pointing to them, in that case, they can't be removed from memory because there's no way to tell the functions that are used to remove them where in the memory they are. Such unreferenced handles are only cleared from memory at the end of a game by a garbage collector (the source of end game delay), but while the game lasts, they accumulate and can cause performance issues.

    Now, there are plenty of functions that create handles. Center of region, random point in region, position of unit, target point of ability being cast... and many others create a location object, which is usually used to create units at, or special effects, or for measuring distances between points... Units in region, units owned by player, units in range,... all these functions give you a Unit Group. Then there are player groups and all other things already mentioned and more.

    You may only need these objects for a moment, but the game can't know that, so they don't just go away by themselves when you no longer need them. The location doesn't just disappear once a unit is created at it. We must use special functions to dispose of the handles we only create temporarily and don't need them to last. The most commonly used are:
    RemoveLocation()
    DestroyGroup()
    DestroyForce()
    ...


    There are more, but these are all an apprentice triggerer really needs to know. There is a GUI action for removing a special effect, so we don't need to know the JASS function for that, units are removed from memory automatically once their corpses decay, and anything else is usually too rare to care about. If we are practical, the only thing we really need to care about are temporary handles that are created dynamically and rapidly, in triggers that run often or loop a lot. So what if a cinematic trigger that runs only once leaks a few locations? It's the tens of thousands of locations or unit groups that can leak on a fast periodic trigger that matter.

    As has already been mentioned, to remove a temporary handle that has served it's purpose, we need to know where it is, we need to have a variable pointing to it. The only way to do that is to point a variable to it when we create it. For that, we first need some variables that we'll use for this purpose, in my examples I'll name them tempPoint and tempGroup. They are global variables that you define in the variable editor, and because of that they get the "udg_" prefix in JASS scripts.

    Now, let's say we want to create a footman at the center of playable map area. If we didn't care about memory leaks, we would do it like this:
    Unit - Create 1 Footman for (Player 1 - Red) at (center of (playable map area)) facing 0.00 degrees

    However, the (center of (playable map area)) creates a location object at which the footman is then created, and this location then "leaks" because we can't remove it, because there's no way to know where in memory it is. We avoid this leak like this:
    Set tempPoint = (center of (playable map area))
    Unit - Create 1 Footman for (Player 1 - Red) at tempPoint facing 0.00 degrees

    Custom script:
    call RemoveLocation( udg_tempPoint )

    and similarly for everything else, let's say we want to kill all units in a region:
    Set tempGroup = units in TheUnholyRegionOfVanillaFlavouredDoom <gen>
    Unit group - pick every unit in tempGroup and do: Kill (picked unit)

    Custom script:
    call DestroyGroup( udg_tempGroup )

    That is about it concerning the basics of memory leaks. Remember not to worry about every petty detail, if you have an old map which you want to fix a bit, just clear the most critical special effect, unit group and location leaks and any noticeable problems with lag will be solved. On future maps, you can afford to be a bit more precise and thorough in hunting memory leaks.

    One last note, it is said that destroying triggers after you no longer need them can be quite effective performance-wise, especially with long initialization triggers that run once and are then not used again. To do that, just put the following line at the end of the trigger.

    call DestroyTrigger(GetTriggeringTrigger())

    Leak Check 1.3 is a handy tool to help find and eliminate memory leaks.
     
    Last edited by a moderator: Mar 28, 2007
  2. troylowyee

    troylowyee

    Joined:
    Apr 13, 2008
    Messages:
    142
    Resources:
    3
    Maps:
    3
    Resources:
    3
    hi i have a question, what if i created units at a region instead? do i have to create a variable?
     
  3. pOke

    pOke

    Joined:
    Mar 24, 2013
    Messages:
    1,067
    Resources:
    1
    Maps:
    1
    Resources:
    1
    If you're doing Center of or Random Point in X Region. Yes you need to do as shown above and Set that in a variable and remove it via call RemoveLocation(udg_myVariable)
     
  4. troylowyee

    troylowyee

    Joined:
    Apr 13, 2008
    Messages:
    142
    Resources:
    3
    Maps:
    3
    Resources:
    3
    Oh thanks i didnt know that, my map lags after awhile and im trying to clear as much leaks as possible!
     
  5. IcemanBo

    IcemanBo

    Joined:
    Sep 6, 2013
    Messages:
    5,848
    Resources:
    22
    Maps:
    3
    Spells:
    11
    Template:
    1
    Tutorials:
    4
    JASS:
    3
    Resources:
    22
    Read this newer thread by Ralle, it explains pretty straight forward how to remove all important objects: Things That Leak
     
  6. troylowyee

    troylowyee

    Joined:
    Apr 13, 2008
    Messages:
    142
    Resources:
    3
    Maps:
    3
    Resources:
    3
    what about pick every unit in (playable map area)?

    Or what about panning the camera to center of region?
     
  7. IcemanBo

    IcemanBo

    Joined:
    Sep 6, 2013
    Messages:
    5,848
    Resources:
    22
    Maps:
    3
    Spells:
    11
    Template:
    1
    Tutorials:
    4
    JASS:
    3
    Resources:
    22
    makes a group

    makes a point/location

    You need to use variables for destroying them after usage, like showed here or in linked thread (linked thread is advised).
     
  8. troylowyee

    troylowyee

    Joined:
    Apr 13, 2008
    Messages:
    142
    Resources:
    3
    Maps:
    3
    Resources:
    3
    I have read the thread, its really useful but if the pan camera always leaks then what can i use to pan camera? etc when a unit spawns and you want to pan it to the unit
     
  9. IcemanBo

    IcemanBo

    Joined:
    Sep 6, 2013
    Messages:
    5,848
    Resources:
    22
    Maps:
    3
    Spells:
    11
    Template:
    1
    Tutorials:
    4
    JASS:
    3
    Resources:
    22
    why you say "always leaks" ?
     
  10. Michael the Boombringer

    Michael the Boombringer

    Joined:
    Dec 18, 2016
    Messages:
    102
    Resources:
    0
    Resources:
    0
    What happens if memory leaks?
     
  11. IcemanBo

    IcemanBo

    Joined:
    Sep 6, 2013
    Messages:
    5,848
    Resources:
    22
    Maps:
    3
    Spells:
    11
    Template:
    1
    Tutorials:
    4
    JASS:
    3
    Resources:
    22