HomeUser Control Panel (unavailable in archive)ForumsTutorialsArt GalleryResourcesMaps

Leak questions

10-30-2007, 03:03 AM#1
jw232
What exactly is a leak? What kind of variables need to be nulled to prevent leaks? Also, is making a lot of string variables and assigning values to them bad?

Is nulling u in the loop necessary, or do I just need to do it at the end?
Collapse JASS:
function f takes nothing returns nothing
    local unit u
    loop
        set u=FirstOfGroup(udg_group)
    exitwhen u==null
        call GroupRemoveUnit(udg_group,u)
        //do stuff
        set u=null
    endloop
endfunction
10-30-2007, 03:10 AM#2
Vexorian
A "memory leak" is what we call to the event of using a lot of memory and not disposing it once you don't need it.

You only need to null at the end of functions, and you only need to null local variables (not arguments) of handle types.

And nulling prevents one (minor) kind of memory leak. A more important one is to actually destroy the objects.

Lots of string variables won't hurt your map a lot, but what will certainly cause memory leaks would be lots of different string values If you keep using the same string values, it will not be a leak, thus you should only worry if you are making a system that for some reason needs to generate a lot of 'random' strings.
10-30-2007, 03:27 AM#3
jw232
Thanks for the answers. So a handle type is everything that isn't an integer, real, boolean, or string right? And handles store references to something else? If I have a trigger that creates a string "player 1 kills player 2" every time a unit dies, would that make a lot of memory leaks? Is there anything I can do about string leaks?

Does u need to be nulled in this?
Collapse JASS:
function f takes nothing returns nothing
    local unit u
endfunction 
10-30-2007, 04:36 AM#4
Ammorth
Only new strings will "leak". Once you create a string again, it won't leak. So, creating many different strings will "leak", using the same strings will not.

Keep in mind, "leak" means the data is in memory, but it is never used again.

What can you do about string leaks? Nothing really.

If u is never set, id doesn';t need to be nulled.

Edit: I wonder how many new strings are created with this function
Collapse JASS:
function test takes nothing returns nothing
    local string s1 = "a" //1
    local string s2 = "b" //2
    local string s3 = "c" //3
    local string s4 = "d" //4
    local string s5 = s1+s2+s3+s4 //5 or 7 ("ab", "abc" , "abcd")
endfunction
10-30-2007, 05:17 AM#5
jw232
So in f1 there's one leak and in f2 there's two leaks right?
Collapse JASS:
function f1 takes nothing returns nothing
    local string s="bla"
    
    set s="blabla"
endfunction

function f2 takes nothing returns nothing
    local string s="bla"
    local string s2="asdhs"
    
    set s="blabla"
endfunction
10-30-2007, 02:40 PM#6
Ammorth
Not really, it depends when the function is ran. Assume this function runs at the start of the map.

Collapse JASS:
function Example takes nothing returns nothing
    local string s = "a" // 1 string
    set s = "b" // 2 strings
    set s = "c" // 3 strings
    set s = "d" // 4 strings
    set s = "a" // no leak since the string was already created from before.
    set s = "c" // same thing, the string was created from before.
    set s = s + "a" // 5 strings (since the string "ca" has never been created before)
endfunction

Now, if you were to call that function again, this is what would happen.

Collapse JASS:
function Example takes nothing returns nothing
    local string s = "a" // No leak, already created from before
    set s = "b" // No leak, already created from before
    set s = "c" // No leak, already created from before
    set s = "d" // No leak, already created from before
    set s = "a" // No leak, already created from before
    set s = "c" // No leak, already created from before
    set s = s + "a" // No leak, already created from before
endfunction

As you can see, once you create a string once, it won't leak again.

It is believed that strings are stored in a hash table. When a new string is used, it is added to the hash table. When the same string is used, it is not created, but used from the hash table.

When you will run into leaks is when you create lots of useless strings.

Collapse JASS:
function Stupid takes nothing returns nothing
    local integer i = 0
    loop
        exitwhen i > 10000
        call BJDebugMsg(I2S(i))
        set i = i + 1
    endloop
endfunction

This function creates 10000 strings which will probably not be used again, or very often, so the strings "leak."

Again, you should not worry about string leaks unless you have a system which revolves around strings.
10-30-2007, 11:17 PM#7
jw232
I see. Now I have a question about groups. Do I have to destroy this group twice or just once? Since it's still just one group right?
Collapse JASS:
function f takes nothing returns nothing
    local group g=CreateGroup()
    call GroupEnumUnitsInRange(g,0,0,100,null)
    //do stuff
    call GroupEnumUnitsInRect(g,GetPlayableMapRect(),null)
    //do more stuff
endfunction
10-31-2007, 12:04 AM#8
Anopob
If you set a group to something then to something else, you just need to destroy and null it once.
10-31-2007, 12:51 AM#9
Ammorth
Actually, if you set a group to something, you should destroy it before you set it to a new group. If you are ADDING more units to the same group, there is no need to null or destroy it in between. Just make sure you clean it all up once you are done.
10-31-2007, 02:16 AM#10
jw232
GroupEnumUnit functions don't set to a new group right?
10-31-2007, 04:15 AM#11
Ammorth
No, since it takes a previously made group as an argument.
11-06-2007, 07:33 PM#12
jw232
Another question, do function parameters need to be nulled?
Collapse JASS:
function f takes unit u returns nothing
    //does u need to be nulled?
endfunction
11-06-2007, 07:40 PM#13
Salbrismind
Quote:
Originally Posted by jw232
Another question, do function parameters need to be nulled?
Collapse JASS:
function f takes unit u returns nothing
    //does u need to be nulled?
endfunction

I just started Jass and im pretty sure it doesn't. Or at least you can't null that variable.
11-06-2007, 07:49 PM#14
cohadar
Geez there are like 10000 tutorials out there about leaking and people still ask about it.

Read the goddamn tutorial section.
11-06-2007, 08:19 PM#15
jw232
Quote:
Originally Posted by cohadar
Geez there are like 10000 tutorials out there about leaking and people still ask about it.

Read the goddamn tutorial section.
I could only find a tutorial on Local Var Leaks. Stfu or answer the question.