Tutorial Understanding Leaks

I

IKilledKEnny

Guest
Understanding Leaks
~By IKilledKEnny~

I’m tired of seeing so many people having a hard time understanding leaks even after they have read leaks removal tutorials, so I have decided to make a short mini-tutorial about it trying to explain it the clearest way possible.

If you didn’t read yet a leak removing tutorial I suggest warmly you check this, this or this because they are much more informative tutorials, here I’m going to explain everything slower so it would be easier to understand.

In this tutorial we will talk about 3 common leaks which are:
[list
[*] Unit Group (Group) - Can cause serious leaks

[*]Player Group (Force) - Not a huge leak but still considerable one.

[*]Point (Location) - A minor leak, however this is the one that is most frequently created.
[/list]

First of all let’s understand what leaks are. Leaks are a piece of data that is not removed, or overwritten without being removed, what causes the computer to store it in a memory and forces it to remember it until removed, clearing leaks actually removes this piece of information after being used. Clearing leaks is a very powerful tool, and clearing things we plan in using in the future and have no way to re-receive could destroy whole triggers’ functions, however clearing leaks is an important unavoidable thing in World Editor which mustn’t be ignored. Integer, Booleans, strings and reals don’t need to be removed as they take minimal memory space.

Conclusion: A leak is a piece of information that hasn’t been removed and forces to computer to remember it which slows its reaction. The most common things that need to be removed are Groups, Forces and Locations.

So after we understood what leaks are, and how important it is to remove them, the next question is how do we detect them? Lets check this following trigger:

Code:
    Events
        Unit - A unit Dies
    Conditions
    Actions
        Unit - Move (Triggering unit) instantly to (Center of (Playable map area))

Can you detect here the leak? No? Let’s bold it.

Unit - Move (Triggering unit) instantly to (Center of (Playable map area))

Do you see that? We used a point but we don’t remove it. Let’s move on.

Code:
    Events
        Time - Every 1.00 seconds of game time
    Conditions
    Actions
        [b]Unit Group - Pick every unit in (Units owned by Player 1 (Red)) and do (Actions)[/b]
            Loop - Actions
                Unit - Remove (Picked unit) from the game

This is another type of leak. Unit group leak, much deadlier then the point leak. What makes it even worse is the fact that the leak repeats every 1 second. In few minutes or even seconds your game would crush because of this little trigger. Finally let’s have a quick look at forces leaks.

Code:
    Events
        Time - Every 1.00 seconds of game time
    Conditions
    Actions
        Player Group - Pick every player in (All players) and do (Actions)
            Loop - Actions
                Player - Add 1 to (Picked player) Current gold

Now you expect me to say it leaks, right? Actually it doesn’t, force all players never leaks, however other forces do leak. So if we change all player to:

(All players controlled by a User player)

We have a serious leak problem.

So, you should get the feeling by now what leaks and what doesn’t. However there is a point I want to make sure you understand. The more the leak repeats itself so it is worse. Thus a leak that will occur every 5 seconds is not nearly as bad as one that repeats every 0.01 seconds.

So we know what are leaks, how to detect them, and when they are the worst. Good. What can we do about it? Well all you need to do is add one minor line and you removed a leak. The first thing you do is open actions, go to -general and pick custom script (Triggers > Actions > -General > Custom Script). When you do that you have a place to type in a command. You might ask yourself, huh? Since when do we use text commands in World Editor?

Well let me tell you a secret, World Editor language, like any other programming language (as far as I know?) is made of text. When you program in World Editor you use a tool called GUI, however it is converted to text, which is called JASS. So when you open custom script you write down a JASS command.

Now the next question we ask is why do we use JASS command and not the GUI version of the command? Well there is a simple answer, not all JASS commands can be found in GUI, and removing leaks actions can’t be found in GUI so we must type them in JASS.

Ok, so now we understand why we have to type down the command and not simply find it in one of GUI’s many charts. But now you must wonder, what do we type? We need to type:

call command(udg_variable’s name[array])

What does that mean? Everything in bold will always be inside what you type (unless you use locals, you don’t need to know what that means now, just keep in mind that if you use local variable you need to remove the udg_).

So the first word is call. Call means we call a native function, which is a function that JASS can recognize. In other words, call starts the remove leak line. Next we put the command. The command is either RemoveLocation when we talk about point, DestroyForce when we talk about player group or DestroyGroup when we talk about a unit group. Next we put in (udg_ and then the variable’s name exactly like it is in the variable editor. If there is an array we type it and wrap it in [] and then to end the line we put a ). Make sure that there are no spaces between anything but the space between call and the command.

Now if you read this carefully you might have seen that you can only remove variable. What this mean? Let’s take our first example.

Code:
    Events
        Unit - A unit Dies
    Conditions
    Actions
        Unit - Move (Triggering unit) instantly to (Center of (Playable map area))

And we want to remove the leak so we a start typing.

call RemoveLocation(udg_....), but what do we put where there is ….? How do we define the point? We can’t that’s why we have to put it inside a variable. So how would the trigger look like when properly removing the leak? Like this:
Code:
 Untitled Trigger 001
    Events
        Unit - A unit Dies
    Conditions
    Actions
        Set PointVariable = (Center of (Playable map area))
        Unit - Move (Triggering unit) instantly to PointVariable
        Custom script:   call RemoveLocation(udg_PointVariable)

We set a point variable (PointVariable) to the wanted point, do with the point something and then remove it. In the same manner we do with Player Groups and Unit groups while changing variable type and the command after the call to DestroyForce or DestroyGroup.

Let’s see for a second however how can leaks removal destroy a trigger.

Code:
 Untitled Trigger 001
    Events
        Unit - A unit Dies
    Conditions
    Actions
        Set PointVariable = (Center of (Playable map area))
        Custom script:   call RemoveLocation(udg_PointVariable) 
        Unit - Move (Triggering unit) instantly to PointVariable

You see what we did here? We remove the point (thus the leak) before we used it. So when we order the unit to move to PointVariable, we removed it already from the computer’s memory, so now he can’t recognize it and it will return null ("nothing" in JASS language), thus the unit will not be moved.

Let’s check quickly the same example when using an array.

Code:
 Untitled Trigger 001
    Events
        Unit - A unit Dies
    Conditions
    Actions
        Set PointVariable[b][1][/b] = (Center of (Playable map area))
        Unit - Move (Triggering unit) instantly to PointVariable[B][1][/B]
        Custom script:   call RemoveLocation(udg_PointVariable[b][1][/b])

Very simple. Again there is no difference between locations, groups and forces but the fact that we change the first word that comes after call.

Important note: YOU MAY NOT destroy a unit group inside itself and the same thing goes to player group. This is ILLIGAL:
Code:
 Untitled Trigger 001
    Events
        Unit - A unit Dies
    Conditions
    Actions
        Set GroupVar = (Units in (Playable map area))
        Unit Group - Pick every unit in GroupVar and do (Actions)
            Loop - Actions
                Unit - Remove (Picked unit) from the game
                Custom script:   call DestroyGroup(udg_GroupVar)

This is ok:

Code:
 Untitled Trigger 001
    Events
        Unit - A unit Dies
    Conditions
    Actions
        Set GroupVar = (Units in (Playable map area))
        Unit Group - Pick every unit in GroupVar and do (Actions)
            Loop - Actions
                Unit - Remove (Picked unit) from the game
        Custom script:   call DestroyGroup(udg_GroupVar)

You see? The removing line is OUTSIDE the loop actions.

So you might now say to yourself, huh this is really easy, why didn’t I understand it before? Well it is easy until we reach loop, which makes it a little more complicated, but still very easy once you understand the basics. Let’s start with a little challenge, what do you think out of those 2 would leak?

Code:
 Untitled Trigger 001
    Events
        Unit - A unit Dies
    Conditions
Actions
    Set GroupVar = (Units in (Playable map area))
        Unit Group - Pick every unit in GroupVar and do (Actions)
            Loop - Actions
                Set PointVariable = (Position of (Picked unit))
                Unit - Move (Triggering unit) instantly to PointVariable
                Custom script:   call RemoveLocation(udg_PointVariable)
        Custom script:   call DestroyGroup(udg_GroupVar)

Or

Code:
 Untitled Trigger 001
    Events
        Unit - A unit Dies
    Conditions
Actions
    Set GroupVar = (Units in (Playable map area))
        Unit Group - Pick every unit in GroupVar and do (Actions)
            Loop - Actions
                Set PointVariable = (Position of (Picked unit))
                Unit - Move (Triggering unit) instantly to PointVariable
        Custom script:   call RemoveLocation(udg_PointVariable)
        Custom script:   call DestroyGroup(udg_GroupVar)

If you thought that the second trigger would leak you are 100% right. Now why does it leak? If you notice we do: pick every unit in all units in the entire map. Thus we are most likely to the see actions inside the loop more then once. What does it mean? It means that rewrite PointVariable for every unit there is on the map, which causes a big leak. And then we remove the PointVariable OUTSIDE the loop, what will remove only the last defined PointVariable, what makes it utterly useless. However when we put the remove leak inside the loop, for each pointvariable we get, we also remove it, what stops the leaks.

JASS

Due do requests I decided to add this section and go over it quickly. First of all let's start with locals. If you started to learn JASS you probably know what local is. However there is a different method of removing locals. What we do is doing the exact same thing but we remove th udg_. udg_ means it is a global variable (user defined global), however when we talk about locals we don't want the computer to think we are talking about a global becuase it is a local variable. So what do we do?

call command(variable's name)

No udg_ as you can see.

Also few JASS commands that might help you.

Integer A in GUI = bj_forLoopAIndex

Integer B in GUI = bj_forLoopBIndex

Player number in GUI = GetConvertedPlayerId(takes player) // Palyers from JASS range from 0=11! 0=1, 1=2 etc. So if you get player's id I suggest you do GetConvertedPlayerId(some player) + 1.

Owner of unit in GUI = GetOwningPlayer(takes unit)

Triggering unit in GUI = GetTriggerUnit() // You must have the ()

Triggering player in GUI = GetTriggerPlayer() // You must have the ()

Lastly when talking about JASS we need to null handles (widgets(units,players,items),boolexprs etc.) this will be done by doing:

set Im_A_Handle = null
 

elmstfreddie

The Finglonger
Reaction score
203
I already know everything about leaks, so I got bored pretty quickly :p
ANYWAYS, I don't think you could explain it anymore, I think the biggest issue is that people are scared of using custom script in their GUI. Hard to emphasis how freaking easy it is to use it to remove memory leaks though :p
Since you took the time and it's well written you get my reppage :)
 
S

Scourgey

Guest
Might be worthwhile posting about how local variables in JASS are dealt with. In particularly nullifying the local variables.
 

Ghan

Administrator - Servers are fun
Staff member
Reaction score
889
Another thing might be to go over how to use different Integers for variables inside the script. What if someone wanted to use Integer A? Anyway, +rep.
 
I

IKilledKEnny

Guest
Thanks for the feedback.

I will show how to deal with locals and few basic JASS integers, however, that will have to wait for tommorow.

And more feedback is appreciated, good or bad.
 

Duwenbasden

Ver 6 CREATE energy AS SELECT * FROM u.energy
Reaction score
165
I want to get this off my mind...

when I do this:
Code:
local location loc_thing
set loc_thing = (Units in (SOME_REGION))
set loc_thing = (Units in (Playable map area))

does it mean:
Code:
location loc_thing
loc_thing = new location (Units in (SOME_REGION))
loc_thing = new location (Units in (Playable map area))
or
Code:
location loc_thing = new location
loc_thing = Units in (SOME_REGION)
location loc_thing = (Units in (Playable map area))
?


Leaks are a piece of data that is not removed, or overwritten without being removed
Overwritten data does not leak.

, what causes the computer to store it in a memory and forces it to remember it until removed,
that has 0 impact on performance, unless you are running out of memory.

Integer, Booleans, strings and reals don’t need to be removed as they take minimal memory space.

No, they don't leak because they are primitive types (static).

A leak is a piece of information that haven’t been removed and forces to computer to remember it which slows its reaction.

-_-
 

phyrex1an

Staff Member and irregular helper
Reaction score
447
>that has 0 impact on performance.
In a real computer environment then yes, as long as you keep away from the swap. The jass engien however isn't a 'real computer environment' and have it's own tracker of objects that will most likely be slower if there are many handles floating around.

>No, they don't leak because they are primitive C++ types (static).
Since when does jass integer == C++ integer? Don't mix jass and c++ only because jass is most likely parsed and executed by a program written in c++.

>when I do this:
In GUI and what the resulting jass code looks like?
set udg_loc_thing = <whatever blizzard.j function is>
 

PurgeandFire

zxcvmkgdfg
Reaction score
509
Nice, a lot of questions in there, I understood all of it and I already knew it, but I like to read (Actually Skim :D ) through tutorials, good job. +rep :)
 
I

IKilledKEnny

Guest
Added at the end of the tutorial a short jass section which covers removing locals and few return type (GetTriggerUnit() or bj_forLoopAIndex and such).
 

chovynz

We are all noobs! in different states of Noobism!
Reaction score
130
Conclusion: A leak is a piece of information that haven’t been removed and forces to computer to remember it which slows its reaction. The most common things that need to be removed are Groups, Forces and Locations.

Why is that important?

IKilledKEnny said:
So after we understood what leaks are, and how important it is to remove them, the next question is how do we detect them? Lets check this following trigger:

You didn't explain that well to (insert name of someone who still doesn't know why it's important to remove them.) You did explain it, but in a technical form.

Ok, so it is important. Why? You didn't mention that in a "understanding" way. (i.e causes lagging, delay for players who are not the host, and in extreme cases, crashes/disconnects etc... all of which are very frustrating to any player of a game that suffers from these.) Might want to add something in. :)

Really good Tut. ;) I can't + rep you.:(
 

Tom Jones

N/A
Reaction score
437
>when I do this:

Code:
local location loc_thing
set loc_thing = (Units in (SOME_REGION))
set loc_thing = (Units in (Playable map area))
does it mean:

Code:
location loc_thing
loc_thing = new location (Units in (SOME_REGION))
loc_thing = new location (Units in (Playable map area))
or

Code:
location loc_thing = new location
loc_thing = Units in (SOME_REGION)
location loc_thing = (Units in (Playable map area))

The bolded one.

>Overwritten data does not leak.
True, but how in the world are you going to overwrite anything with jass (Without the use of gamecaches)? Each time we create a object it'll be stored in the memory, the variable is just a pointer. If you change the pointer the data isn't getting overwritten, the pointer simply points somewhere else.

>Comments
Mention the importance of udg_ in front of the variable name, and describe why you have to add it in front of the variable name.

Another way of removing group leaks in GUI is by setting the global blizzard boolean bj_wantDestroyGroup to true, which I personally prefer (When I'm forced to make GUI scripts).

The jass "section" is not good enough. Those comments doesn't even relate to the subject, and there are no examples. If I didn't know jass, I would still be clueless after reading the section.

Some statements and assumptions that isn't correct, but it's okay, one gets the picture.

Other than that, it's pretty decent, keep it up :)
 

esb

Because none of us are as cruel as all of us.
Reaction score
329
Wow, thanks, it really helped. So maybe this is the reason i disconnect a lot from custom games, especially ones like LOAP/RL.
Also, by "crush" the computer, you mean crash, disconnect, lag, etc.? (I know it's not literally crush)

Now i see why people say "I can't live without variables" Cause you use them even in center of region :p

Few Questions:

Is it case sensitive?

Does it matter if when i create/move/order/etc. and object to a point, if it's random, specific, or center? Will it still be
Custom script: call RemoveLocation(udg_POINT)
(replacing POINT with my location/point)

How do i set a random point to a variable? (Nevermind, i think i found it)
Also, hope i got it right...:

Every time i Order/Create/Move/etc. an Object to a Point (Example: Such as Center of Region X) I Add
Custom script: call RemoveLocation(udg_Region X)

With Arrays, i add the Custom Script like i regularly would, and the [#] i used included right?

In Loops, always if the Location is before the loop, we do the Custom Script OUTSIDE the loop, if the Location is inside the loop, we do the Custom Script INSITE the loop.

Hmmm, sounds easy :D
 
I

IKilledKEnny

Guest
esb, I'm suprised, most mappers don't catch that fast!;)

Yes it's case sensitive.

It doesn't matter what is the type of the loc/group/force as long as you store it in a variable and them remove it afterwards.

Every time i Order/Create/Move/etc. an Object to a Point (Example: Such as Center of Region X) I Add
Custom script: call RemoveLocation(udg_Region X)

Now that will remove the region you would do:

loc_point = center of region X
move unit to loc_point
call RemoveLocation(udg_loc_point)

rest is right.
 

PurgeandFire

zxcvmkgdfg
Reaction score
509
lol.... Not to be rude at all or offensive or anything, but I think you should get a spell checker or something... Sorry, :D . I'd rep you for this but I can't yet!! :D
 
I

IKilledKEnny

Guest
lol.... Not to be rude at all or offensive or anything, but I think you should get a spell checker or something... Sorry, :D . I'd rep you for this but I can't yet!! :D

English is second language, I write all my tutorials in 'Word' and triple / double check them. I will run this again, though, if you found many mistakes. And don't worry you are far from being offensive / rude.
 

esb

Because none of us are as cruel as all of us.
Reaction score
329
It's still read-able :p
I corrected some of it, i think they are right :p
I forgot to show which word is incorrect... guess you will have to ALT + F
======
tried....
Leak removal tutorials.....
about it, trying to...
If you haven't read a leak removing tutorial yet, I suggest...
Conclusion: ...piece of information that hasn't been... forces the computer...
So after we have understood what leaks are,...
Can you detect the leak here? No?...
...We used a point, but we didn't removed it. ...
The more the leaks repeats itself, the worse it is.
Ok, so now we understand why, we have to type down the command...
How do we define the point? We can't, that's why we have...
You see what we did here? We removed the point...
This is illegal
You see? The removing line is outside....
=======
 

SerraAvenger

Cuz I can
Reaction score
234
First of all let’s understand what leaks are. Leaks are a piece of data that is not removed, or overwritten without being removed, what causes the computer to store it in a memory and forces it to remember it until removed, clearing leaks actually removes this piece of information after being used. Clearing leaks is a very powerful tool, and clearing things we plan in using in the future and have no way to re-receive could destroy whole triggers’ functions, however clearing leaks is an important unavoidable thing in World Editor which mustn’t be ignored. Integer, Booleans, strings and reals don’t need to be removed as they take minimal memory space.

Not quite accurate.
This should be more correct.
SerraAvenger said:
Memory Leak:
A memory leak occurs, when you loose the last pointer to an object that actually uses memory. As you can no longer refer to that object then, you cannot destroy and so you cannot free the memory you allocated by creating it. This memory can no longer be used, so you have small to great perfomance losses.
Note that as long you still can refer to the object (due to variables or functions), the memory it occupies will not be lost due a leak. Still, you should destroy every object as soon as you no longer need it; saving in variables will need even more memory than a simple memory leak. Still you can free the memory and reduce the needed ressources.

You mainly seperate between 2 forms: Normal leaks, and Bad leaks. A bad leak is a leak that happens multiple times (in a loop , in a trigger fireing very often ). Normal leaks are such that only occur once ( In a trigger on map initiallisation creating a point for A unit that shall patrol to that point ).

Memory leaks only at the moment you loose the last pointer to the object allocating it, not before and not after. And an object ( location, p.e. ) cannot leak. It is the memory that does, and an object cannot have a leak, it is the code that has.
 
B

brulogaz

Guest
well, its fun how you state that having some leaks will crash the computer. I mean... my comp is not a bomb but having some undeleted variables on warcraft won't cause it to crash =/ Its kinda being an obsession...

I agree that you should free the variables in a loop, but the rest mostly won't ever affect the game. Im not a grandmaster programmer but i did some programmation in many languages or even for games emulators and i was surprised to see all this obsession for leaks when i began to take part to the map-maker community in warcraft. So i wanted to try it myself and unless your map has lot and lot of complicated/looping/neverending triggers i don't see much a problem in the leaks.

I tried the exemple in your first post that would "crash the game in a few minuts" i made the loop to 0.001 second and i did not even lose .1 fps. (But i doubt its a good exemple since the data is probably just being overwrited)...

Whatever, il stay very septical toward the "leaks phenomen" until il see some proof that leaving some variable eat 1kb of my ram will slow down the game. But hey, i agree that leaving memory holes in a loop can crash the game, but everybody on this forum seem to interpretate an unremoved variable as something very evil and dangerous.

As any programmer with a little experience, i try to optimize my codes etc. But most of the time its just by "reflex" or to make the code look clean and not something mandatory to make it run smooth.

Btw: English is my second language;)
 

djpsycho

New Member
Reaction score
0
i have like ... when i want to try my map it stops at variables and then it gives me like 150errors and doesn't let me start map (it only starts TFT)
 

luke20054

New Member
Reaction score
3
thx v v much for leak infromation it was simple and easy to understand and in will update map with this new piece of info, thx again, and could u plz do a tutorial on jazz as i am rly keen to learn how u do it.
 
General chit-chat
Help Users
  • No one is chatting at the moment.
  • WildTurkey WildTurkey:
    is there a stephen green in the house?
    +1
  • The Helper The Helper:
    What is up WildTurkey?
  • The Helper The Helper:
    Looks like Google fixed whatever mistake that made the recipes on the site go crazy and we are no longer trending towards a recipe site lol - I don't care though because it motivated me to spend alot of time on the site improving it and at least now the content people are looking at is not stupid and embarrassing like it was when I first got back into this like 5 years ago.
  • The Helper The Helper:
    Plus - I have a pretty bad ass recipe collection now! That section of the site is 10 thousand times better than it was before
  • The Helper The Helper:
    We now have a web designer at my job. A legit talented professional! I am going to get him to redesign the site theme. It is time.
  • Varine Varine:
    I got one more day of community service and then I'm free from this nonsense! I polished a cop car today for a funeral or something I guess
  • Varine Varine:
    They also were digging threw old shit at the sheriff's office and I tried to get them to give me the old electronic stuff, but they said no. They can't give it to people because they might use it to impersonate a cop or break into their network or some shit? idk but it was a shame to see them take a whole bunch of radios and shit to get shredded and landfilled
  • The Helper The Helper:
    whatever at least you are free
  • Monovertex Monovertex:
    How are you all? :D
    +1
  • Ghan Ghan:
    Howdy
  • Ghan Ghan:
    Still lurking
    +3
  • The Helper The Helper:
    I am great and it is fantastic to see you my friend!
    +1
  • The Helper The Helper:
    If you are new to the site please check out the Recipe and Food Forum https://www.thehelper.net/forums/recipes-and-food.220/
  • Monovertex Monovertex:
    How come you're so into recipes lately? Never saw this much interest in this topic in the old days of TH.net
  • Monovertex Monovertex:
    Hmm, how do I change my signature?
  • tom_mai78101 tom_mai78101:
    Signatures can be edit in your account profile. As for the old stuffs, I'm thinking it's because Blizzard is now under Microsoft, and because of Microsoft Xbox going the way it is, it's dreadful.
  • The Helper The Helper:
    I am not big on the recipes I am just promoting them - I use the site as a practice place promoting stuff
    +2
  • Monovertex Monovertex:
    @tom_mai78101 I must be blind. If I go on my profile I don't see any area to edit the signature; If I go to account details (settings) I don't see any signature area either.
  • The Helper The Helper:
    You can get there if you click the bell icon (alerts) and choose preferences from the bottom, signature will be in the menu on the left there https://www.thehelper.net/account/preferences
  • The Helper The Helper:
    I think I need to split the Sci/Tech news forum into 2 one for Science and one for Tech but I am hating all the moving of posts I would have to do
  • The Helper The Helper:
    What is up Old Mountain Shadow?

      The Helper Discord

      Members online

      No members online now.

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top