Memory leak situation

huanAk

New Member
Reaction score
4
Hi

I know how to remove memory leak for basic trigger. but I still don't understand memory leak in follow situation :

Question 1:
what the difference between "remove all unit from temp_group" and "Destory group (temp_group)" ? do first one result leak ?

Question 2:
if you store point into array, :
points_array[1] = location of unit_a
points_array[2] = location of unit_b
do something here
points_array[1] = location of unit_c
points_array[2] = location of unit_d
removeLocation(points_array[1])
removeLocation(points_array[2])

do I need removeLocation in middle ?

Question 3:

if I use hastable to store points (same as question 2, but use hashtable). do I still need to apply removeLocation ?


Question 4 : why remove location cause bug in this situation ?

Global variable :
SpawnPoint[1] = center of region 1
SpawnPoint[2] = center of region 2
...

Trigger :

for every unit A of unit Group_G
temp_point = SpawnPoint[player number of the unit A]
move unit A to temp_point
removeLocation(temp_point)

// bug result, value inside SpawnPoint[] will become null :banghead:



Thank for help
 

NeuroToxin

New Member
Reaction score
46
1. Im not sure, I can't quite remember.
2. Yes, you do, as if you don't then the leftover location from points[1] in the first part will leak.
3. Yes, you do, because it will still leak if you don't remove it, but you can save the locations in different handles in between, then remove all of them at the end.
4. Don't use temp_point then, just do Move unit A to SpawnPoint[Player number of (Player(Integer A))]
 

huanAk

New Member
Reaction score
4
thank for replay.

another question :

what the difference between
temp_point = null vs removeLcoation(temp_point)
 

Ghan

Administrator - Servers are fun
Staff member
Reaction score
888
The Memory Leak - Explained

Everybody knows that there is this mystical presence out there called a "memory leak". An entity to be feared by all would-be map makers. It's the unseen menace that lurks behind your loops and creeps up through the gaps in your conditions and actions. But what are they? And how can we combat them effectively? Well I'm hear to help you out.

1. What is a memory leak?
Simply put, a memory leak is a piece of your system's RAM that is allocated for some purpose (storing a location, for instance) that does not get removed when that item is no longer needed. When you stack up lots of these, they add up to be huge chunks of your RAM that aren't cleared, and so your system must work harder and harder to find clear space to store things.

2. What causes a memory leak?
Many things can cause these phantoms of WC3 code. Whenever you create some object like a group, point, or player without keeping a variable to reference the created object so you can destroy it, you've created a leak. You're allocating some portion of memory and you have no way to get to it after the fact to destroy it. It remains... forever. (At least until you quit your game. :p) Even if you set the variable = null, you haven't cleaned the leak. Take a look:

Here is the setup when you have a unit group variable

tempGroupVariable ------------> 0xcoolmemorylocation

The variable points to the location in memory where the useful data is actually stored. So when you set it to null, this happens:

tempGroupVariable ------------> <nothing>

0xcoolmemorylocation


The data is still there! And now you don't have a handy variable there to reference it. :( That's a leak.
Now there is something interesting here. If you have a group and you always use that one group and don't set the variable to anything new, it won't leak. So for example, if you have a global to track a group of units and then do Add Unit to Group and Remove Unit from Group, that's fine. But the moment you say something like Set unitGroupVariable = <Anything>
BAM! A leak occurs. If you are doing to do that, make sure to destroy the existing group first! Explained here:

3. What can I do about memory leaks?
Clean them! It's easy. For example, instead of (Pick all units in (Playable Map Area)) and do (Cool Actions), use a structure like this:
Use a tempGroup unit group variable and set it = (Pick all units in (Playable Map Area))
Then do For all units in (tempGroup) do (Cool Actions)
Then at the end, do the custom script call: "call DestroyGroup(udg_tempGroup)"

That's it! It's a similar structure for other things that leak. And don't forget to do some searching of this forum if you're looking for a specific way to do it - these questions have been asked many times before. :D


That's not so bad, is it? Victory over memory leaks is just a couple lines of code away!
 

Weep

Godspeed to the sound of the pounding
Reaction score
400
Question 1:
what the difference between "remove all unit from temp_group" and "Destory group (temp_group)" ? do first one result leak ?
Think of it like this: the group is a container, and groups can be in it. The first one still leaves the container in existence, but takes all the units out. The second one removes the entire container. You don't have to do the first before the second, though.

It will leak if you make a new container with that variable, eg. "Set temp_group = Units in..." without having destroyed the old container.

Question 2:
do I need removeLocation in middle ?
Yes.

Question 3:
if I use hastable to store points (same as question 2, but use hashtable). do I still need to apply removeLocation ?
Yes, since you've still saved a point. If you don't remove it, it will stay in memory.


Question 4 : why remove location cause bug in this situation ?
// bug result, value inside SpawnPoint[] will become null :banghead:
Because you set temp_point equal to the same point as SpawnPoint[], both refer to the same point as saved in memory...so removing temp_point is removing the same point that's saved as SpawnPoint[].

Note: this isn't the case if you use something like:
Set temp_point = (Position of (Triggering unit))
Set SpawnPoint[1] = (Position of (Triggering unit))

Here, they are not the same in memory since you've asked for the information twice. It's two points in memory, that happen to be at the same position.

another question :
what the difference between
temp_point = null vs removeLcoation(temp_point)
In the first, you're saying that the variable should not have any value, but the point still exists in memory; you never removed it. The second one actually removes the point from memory.
 

huanAk

New Member
Reaction score
4
Thank all !! you guy are amazing and helpful !

now I can check my code and clean up more leak.
 

huanAk

New Member
Reaction score
4
I just have another question in my mind:

it is safe to apply removeLocation(temp_point) before assign new location to temp_point?

eg :

removeLocation(temp_point) // temp_point may already remove from previous trigger/action
temp_point = location of unit_a
removeLocation(temp_point)

because I use them inside loop (sometime I can't follow loop so I remove them before assign new value).
 

KaerfNomekop

Swim, fishies. Swim through the veil of steel.
Reaction score
613
Why can't you just RemoveLocation(temp_point) at the end of any action that needs it (even in loops where it gets reset)?
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Staff online

      Members online

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top