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

How to remove Memory Leaks IN GUI

Level 10
Joined
Oct 5, 2007
Messages
103
Tutorial

Memory Leaks (In GUI)

I. Content
I. Content
II. Memory Leaks
III. Types of Memory Leaks
IV. How to prevent Leaks (In GUI)
V. An Example of Leakless Trigger (In GUI of course)
VI. Variables


II. Memory Leak
What is Memory Leak?
Memory Leak is something that consume memory while being used by a program when we create an object, such as unit, point, dll.

Three Important points:
1. Memory Leak causes Lag
2. Memory Leak causes Server Split
3. Excessive Memory Leak cause random disconnect

That’s why we must prevent Memory Leak...

III. Types of Memory Leaks
As mentioned above, Memory Leak appear as we create an object. WTF? What are the objects that cause Memory Leaks? Player? Unit? Unit Group?
This is the list objects which leak and do not leak...

Leak:
1. Special Effect
2. Unit Group
3. Player Group
4. Point
5. Region
6. Floating Text
7. Countdown Timer
8. Timer Dialog
9. Widgets (Including: Unit, Item, and Destructables)

There are more but those which mentioned above are the most important.

Do Not Leak:
1. Player
2. Integer
3. Real
4. String

Well, string does leak but nothing can be done to remove it.

IV. How to Prevent Leaks (In GUI)
To prevent memory leak we must destroy the leaking object after being created AND BEFORE BEING OVERWRITED.

We’ll discuss each types of leak one by one.
First,
1. Special Effect
How to prevent leak caused by Special Effect?
In GUI, we usually do this...
Code:
Special Effect Leak
Events
Conditions
Actions
Special Effect - Create a special effect attached to the overhead of (Triggering unit) using Abilities\Spells\Other\TalkToMe\TalkToMe.mdl
This trigger leaks!
To remove the leak, we destroy the special effect.

this is how to do it:
Code:
  • Special Effect No Leak
    • Events
    • Conditions
    • Actions
      • Special Effect - Create a special effect attached to the overhead of (Triggering unit) using Abilities\Spells\Other\TalkToMe\TalkToMe.mdl
      • Special Effect - Destroy (Last created special effect)
However...
The Special Effect will cease/ play death animation only/ will be shown only once.
So how to create a Special Effect with long life span?
Code:
  • Special Effect No Leak
    • Events
    • Conditions
    • Actions
      • Special Effect - Create a special effect attached to the overhead of (Triggering unit) using Abilities\Spells\Other\TalkToMe\TalkToMe.mdl
      • Set tempEffect = (Last created special effect)
      • Wait 5.00 seconds
      • Special Effect - Destroy tempEffect
tempEffect is a Special Effect Variable
This lasts for 5 seconds
However...
Of course this make the ... spell.. (you usually use special effects for spell, don’t you?) not MUI, well array the variable will render the spell MUI or at least PUI.

Second,
2. Unit Group
We often create trigger like this one:
Code:
  • Unit Group Leak
    • Events
    • Conditions
    • Actions
      • Unit Group - Pick every unit in (Units in (Playable map area)) and do (Actions)
        • Loop - Actions
This trigger leaks
First we’ll make a variable to store the unit group, so that we can destroy it later.
Example
Code:
  • Unit Group Leak
    • Events
    • Conditions
    • Actions
      • Set tempGroup = (Units in (Playable map area))
      • Unit Group - Pick every unit in tempGroup and do (Actions)
        • Loop - Actions
The variable is tempGroup...
Now, how to remove the leak?
We destroy the unit group after being used.
But, how to destroy it? There’s no function to destroy Unit Group...

At this point we’ll use Custom Script! (You can use this function in GUI)
We’ll use some function from JASS...
What is JASS? Something that becomes the source of GUI.
However, we’re not to learn about JASS here, so let the Memory Leak Study continue!

We must add Custom Script written in JASS DestroyGroup...
The Example:
Code:
  • Unit Group No Leak
    • Events
    • Conditions
    • Actions
      • Set tempGroup = (Units in (Playable map area))
      • Unit Group - Pick every unit in tempGroup and do (Actions)
        • Loop - Actions
      • Custom script: call DestroyGroup(udg_tempGroup)
WTF?! I CAN’T UNDERSTAND
While we use Custom Script... we can write something there...
simply write "call DestroyGroup(udg_VARIABLEUNITGROUP)"
and remember it’s CASE SENSITIVE
Oh, WTF is udg?
udg = User Defined Global
That’s it,
Unit Group doesn’t leak anymore!

3. Player Group
It wasn’t far off from Unit Group
If you understand the Unit Group trigger, this one won’t be so hard...

The trigger bellow leaks:
Code:
  • Player Group Leak
    • Events
    • Conditions
    • Actions
      • Player Group - Pick every player in (All players controlled by a Computer player) and do (Actions)
        • Loop - Actions
It leaks,
First, we set it into a variable...
Code:
  • Player Group Leak
    • Events
    • Conditions
    • Actions
      • Set tempForce = (All players controlled by a Computer player)
      • Player Group - Pick every player in tempForce and do (Actions)
        • Loop - Actions
tempForce is Player Group variable...
Now, we use Custom Script function... again...
We must add Custom Script and write DestroyForce...
This is how to do it:
Code:
  • Player Group No Leak
    • Events
    • Conditions
    • Actions
      • Set tempForce = (All players controlled by a Computer player)
      • Player Group - Pick every player in tempForce and do (Actions)
        • Loop - Actions
      • Custom script: call DestroyForce(udg_tempForce)
It doesn’t leak anymore.

It’s simple, isn’t it?
Now to the fourth object....

4. Point
We usually make this trigger:
Code:
  • Point Leak
    • Events
    • Conditions
    • Actions
      • Unit - Move (Triggering unit) instantly to (Center of (Playable map area))
As usuall, convert it into a variable.
Code:
  • Point Leak
    • Events
    • Conditions
    • Actions
      • Set tempPoint = (Center of (Playable map area))
      • Unit - Move (Triggering unit) instantly to tempPoint
tempPoint is point variable...
This time we use Custom Script too...
The function is: RemoveLocation
Code:
  • Point No Leak
    • Events
    • Conditions
    • Actions
      • Set tempPoint = (Center of (Playable map area))
      • Unit - Move (Triggering unit) instantly to tempPoint
      • Custom script: call RemoveLocation(udg_tempPoint)
It also doesn’t leak anymore!
It’s easy, isn’t it?

Do not be careless...
It hasn’t finished yet...
Have you ever seen this?
Code:
  • Point Leak
    • Events
    • Conditions
    • Actions
      • Set tempPoint = ((Center of (Playable map area)) offset by 256.00 towards 0.00 degrees)
      • Unit - Move (Triggering unit) instantly to tempPoint
      • Custom script: call RemoveLocation(udg_tempPoint)
It LEAKS, because we make Center of Playable map area...
It means.., we it needs to be destroyed too.
How?
Code:
  • Point No Leak
    • Events
    • Conditions
    • Actions
      • Set tempPoint2 = (Center of (Playable map area))
      • Set tempPoint = (tempPoint2 offset by 256.00 towards 0.00 degrees)
      • Unit - Move (Triggering unit) instantly to tempPoint
      • Custom script: call RemoveLocation(udg_tempPoint2)
      • Custom script: call RemoveLocation(udg_tempPoint)
  • That’s the way to do it!
Now, to the fifth object....

5. Region
For this section
We’ll combine it with others
This is a Leaking Trigger:
Code:
  • Region Leak
    • Events
    • Conditions
    • Actions
      • Set tempPoint = (Position of (Triggering unit))
      • Set tempGroup = (Units in (Region centered at tempPoint with size (500.00, 500.00)))
      • Unit Group - Pick every unit in tempGroup and do (Actions)
        • Loop - Actions
      • Custom script: call RemoveLocation(udg_tempPoint)
      • Custom script: call DestroyGroup(udg_tempGroup)
There’s a region that leaks there.
while tempGroup is being set...
we create something but forget to remove it
to remove it... we must convert it into a variable...
Code:
  • Region Leak
    • Events
    • Conditions
    • Actions
      • Set tempPoint = (Position of (Triggering unit))
      • Set tempRegion = (Region centered at tempPoint with size (500.00, 500.00))
      • Set tempGroup = (Units in tempRegion)
      • Unit Group - Pick every unit in tempGroup and do (Actions)
        • Loop - Actions
      • Custom script: call RemoveLocation(udg_tempPoint)
      • Custom script: call DestroyGroup(udg_tempGroup)
We also use Custom Script here
the function is: RemoveRect
Code:
  • Region No Leak
    • Events
    • Conditions
    • Actions
      • Set tempPoint = (Position of (Triggering unit))
      • Set tempRegion = (Region centered at tempPoint with size (500.00, 500.00))
      • Set tempGroup = (Units in tempRegion)
      • Unit Group - Pick every unit in tempGroup and do (Actions)
        • Loop - Actions
      • Custom script: call RemoveLocation(udg_tempPoint)
      • Custom script: call RemoveRect(udg_tempRegion)
      • Custom script: call DestroyGroup(udg_tempGroup)
6, 7, 8. Floating Text
For Floating Text, Countdown Timer, and Timer Dialog...
It’s easy since there’s the function to destroy it in GUI.

9. Widgets
For Unit, Item, and Destructables...
To remove the leaks we must to Remove it... not killing it...
But usually we won’t remove it because we need it until the games end.

V. An Example of Leakless Trigger
Code:
  • Good LeakLess Trigger
    • Events
      • Unit - A unit Is attacked
    • Conditions
      • (Triggering unit) Equal to UnitYangSangatIMBA
    • Actions
      • Set tempUnit = (Attacking unit)
      • Wait 5.00 seconds
      • Set tempPoint = (Position of tempUnit)
      • Set tempRegion = (Region centered at tempPoint with size (256.00, 256.00))
      • Set tempGroup = (Units in tempRegion)
      • Unit Group - Pick every unit in tempGroup and do (Actions)
        • Loop - Actions
          • Unit - Kill (Picked unit)
      • Custom script: call RemoveRect(udg_tempRegion)
      • Custom script: call DestroyGroup(udg_tempGroup)
      • Set tempRegion = (Region centered at tempPoint with size (96.00, 96.00))
      • Set tempGroup = (Units in tempRegion)
      • Unit Group - Pick every unit in tempGroup and do (Actions)
        • Loop - Actions
          • Unit - Remove (Picked unit) from the game
      • Custom script: call RemoveLocation(udg_tempPoint)
      • Custom script: call RemoveRect(udg_tempRegion)
      • Custom script: call DestroyGroup(udg_tempGroup)
VI. Variables
Variable do not remove Leaks.
Variable only help us to give a name to an object,
so we can destroy it later.
Sometimes we can create object (for example: special effect) and instantly destroy it (last created special effect) without using wait function. It doesn’t leak.
In conclusion, variable is only a helping tool, only by removing the object, we can remove leak.

Some use of Variable:
1. To accelerate execution of Action.
If we need "Event Response - Attacking Unit" more than once,
It will be better if we Set it into a Variable...
2. Data Storing
We can store unit "Event Response - Attacking Unit". If we use “Action - Wait”,
"Event Response - Attacking Unit" doesn’t apply anymore.
So we must set it into a variable in the beginning of the trigger before we use the function Wait.

***END OF TUTORIAL***
THX for reading this Tutorial

Credit to:
-Thanatos-
KingB00ker (translator)
 
Last edited:
Level 21
Joined
Aug 21, 2005
Messages
3,699
Players leak too. It's just that you never want to remove them in a game. In fact, anything not in the following list does leak:

- boolean
- integer
- real
- string
- code

Your explanation of what a leak is almost sounds magical... It doesn't explain a lot by saying it's "something that eats your memory", like a cookie monster that lives inside your pc :p
 
Level 31
Joined
May 3, 2008
Messages
3,155
*edited*

Thank you for reminding me, but it would have been better if you had also posted the part that violated the rule.

Are you kidding? The pin up thread already written clearly.

To Eleandor : Hate to say it, but sound as well. :p

Then again, this tutorial would be having hard time to be approve.

1) Too many tutorial like this.
2) hvo-busterkomo have tutorial like this which are far better since it explain jass as well.
 
Level 10
Joined
Oct 5, 2007
Messages
103
Are you kidding? The pin up thread already written clearly.

To Eleandor : Hate to say it, but sound as well. :p

Then again, this tutorial would be having hard time to be approve.

1) Too many tutorial like this.
2) hvo-busterkomo have tutorial like this which are far better since it explain jass as well.

Well this one is the most suitable for beginners IMO.
And this tutorial is exclusively for GUI users.
Oh yes his tutorial is better IN YOUR OPINION
 
Last edited:
Top