1. Updated Resource Submission Rules: All model & skin resource submissions must now include an in-game screenshot. This is to help speed up the moderation process and to show how the model and/or texture looks like from the in-game camera.
    Dismiss Notice
  2. DID YOU KNOW - That you can unlock new rank icons by posting on the forums or winning contests? Click here to customize your rank or read our User Rank Policy to see a list of ranks that you can unlock. Have you won a contest and still havn't received your rank award? Then please contact the administration.
    Dismiss Notice
  3. The Lich King demands your service! We've reached the 19th edition of the Icon Contest. Come along and make some chilling servants for the one true king.
    Dismiss Notice
  4. The 4th SFX Contest has started. Be sure to participate and have a fun factor in it.
    Dismiss Notice
  5. The poll for the 21st Terraining Contest is LIVE. Be sure to check out the entries and vote for one.
    Dismiss Notice
  6. The results are out! Check them out.
    Dismiss Notice
  7. Don’t forget to sign up for the Hive Cup. There’s a 555 EUR prize pool. Sign up now!
    Dismiss Notice
  8. The Hive Workshop Cup contest results have been announced! See the maps that'll be featured in the Hive Workshop Cup tournament!
    Dismiss Notice
  9. Check out the Staff job openings thread.
    Dismiss Notice
Dismiss Notice
60,000 passwords have been reset on July 8, 2019. If you cannot login, read this.

Things That Leak

Discussion in 'Triggers & Scripts' started by Ralle, Jun 10, 2007.

  1. spiwn

    spiwn

    Joined:
    Apr 27, 2008
    Messages:
    1,228
    Resources:
    0
    Resources:
    0
    That does not leak.
    Advice: There is no need to use arrayed variables here, as the object does not have to exist for more than a single trigger execution withouth waits. So you could:
    • Then - Actions
      • Set temp_location = (Position of (Attacked unit))
      • Special Effect - Create a special effect at ShootingLoc[(Player number of (Owner of (Attacked unit)))] using Environment\LargeBuildingFire\LargeBuildingFire1.mdl
      • Special Effect - Destroy (Last created special effect)
      • Custom script: call RemoveLocation(udgtemp_location)


    It wont improve performance, put will keep simple triggers ... well ... simple.

    P.s. Try using compleate sentances in your posts. And try to express yourself in a way that anyone could understand you. Because franky I couldn't ;)
     
  2. klemen

    klemen

    Joined:
    Sep 24, 2007
    Messages:
    301
    Resources:
    0
    Resources:
    0
    Ok, how about the second one?
     
  3. Rvsoldier

    Rvsoldier

    Joined:
    Oct 18, 2007
    Messages:
    141
    Resources:
    0
    Resources:
    0
    Anyone got any advice for me x-x?
     
  4. DoubleZeta

    DoubleZeta

    Joined:
    Nov 9, 2008
    Messages:
    19
    Resources:
    0
    Resources:
    0
    I see... once the action is done, I need to remove it from the game. So that it won't leak and add to the lag.
     
  5. SanKakU

    SanKakU

    Joined:
    May 11, 2008
    Messages:
    1,183
    Resources:
    1
    Maps:
    1
    Resources:
    1
    your trigger looks like you put a lot of effort into it...but i don't really understand complicated gui.

    that's only one reason why i've begun to learn jass.

    and now i'm experiencing awesome results in trying to fix problems large and small by recoding my triggers in vjass.

    if you haven't started learning jass already, i recommend it. and let me link you to this super awesome tutorial that really helped me a lot with my jass programming. [Tutorial] GUI -> vJASS - The Helper Forums

    anyway my guess is that if you write your code in vjass it will be easy to find out what is wrong with the trigger.
     
  6. Kercyn

    Kercyn

    Joined:
    Feb 14, 2009
    Messages:
    850
    Resources:
    0
    Resources:
    0
    • Region 1
      • Events
        • Unit - A unit enters Down Left One <gen>
      • Conditions
        • (Owner of (Triggering unit)) Equal to Player 6 (Orange)
      • Actions
        • Unit - Order (Triggering unit) to Move To (Center of Down Left Two <gen>)


    I wrote this trigger for my TD map. Does it leak? Diablo-dk mentions that moving a unit leaks, but what about ordering it to move?
     
  7. Just_Spectating

    Just_Spectating

    Joined:
    May 16, 2007
    Messages:
    7,023
    Resources:
    46
    Models:
    11
    Icons:
    9
    Skins:
    26
    Resources:
    46
    yes,

    The Location (Center of Down Left Two) leaks. Set the point into a variable, move the unit to that variable, then remove the variable. call RemoveLocation(udg_Your variable here)
     
  8. Kercyn

    Kercyn

    Joined:
    Feb 14, 2009
    Messages:
    850
    Resources:
    0
    Resources:
    0
    Oh no! That means even more boring writing! Never mind, thanks a lot :)
     
  9. mecatronic

    mecatronic

    Joined:
    Jun 20, 2005
    Messages:
    112
    Resources:
    1
    Maps:
    1
    Resources:
    1
    on some tutorials I've read that to get rid of location leaks, you have to set the var to null:
    Code (vJASS):
    set udg_loc = null


    but on others, they use the remove location:
    Code (vJASS):
    call RemoveLocation(udg_loc)


    and on other, they use both:
    Code (vJASS):
    set udg_loc = null
    call RemoveLocation(udg_loc)


    so, which one is right (and why)?
     
  10. Justify

    Justify

    Joined:
    Mar 16, 2008
    Messages:
    935
    Resources:
    5
    Models:
    1
    Icons:
    1
    Spells:
    3
    Resources:
    5
    None and the last a bit :p
    You have to destroy it and null it, but after nulling you can't destroy it anymore:
    Code (vJASS):
    call RemoveLocation(udg_loc)
    set udg_loc = null

    This is correct :)
     
  11. mecatronic

    mecatronic

    Joined:
    Jun 20, 2005
    Messages:
    112
    Resources:
    1
    Maps:
    1
    Resources:
    1
    I don't get this..

    If I first destroy the variable (meaning it can't be used again), how can I just 'use' it one last time to nullify it?
     
  12. Eleandor

    Eleandor

    Joined:
    Aug 21, 2005
    Messages:
    3,681
    Resources:
    2
    Models:
    1
    Tutorials:
    1
    Resources:
    2
    If you use any variable in jass or GUI that is not an integer, real, boolean or string, it is a "pointer".

    A "pointer" points to a place in your RAM memory. Some pointers point to locations, which is a piece of RAM memory that contains a real X coordinate and a real Y coordinate.

    Those X and Y coordinates thus take up memory in your RAM. If you want to remove it (because you no longer need it and want to use the memory for more useful purposes), you must manually do so. This is done through:
    call RemoveLocation(pointer)
    So our "location" (in jass) or "Point" (in GUI) is a pointer to the X and Y coordinate in your RAM memory. RemoveLocation will follow this pointer and then remove the X and Y from your memory.

    Whenever we call RemoveLocation on such a pointer, the memory is cleared of the ADRESS where the pointer points to. The actual pointer is STILL there! (ofcourse, because if it WOULD be removed, you would never be able to use a "Point" variable in GUI again once you removed it!)
    Whenever you use
    set aPointvariable = anotherpoint
    , you are actually changing the pointer so it now points to a different place in memory that contains a different X and Y coordinate.
    This means we have following situation in your memory:

    Adress --- value

    1 --- X coordinate of location 1
    ----- Y coordinate of location 1
    3 --- X coordinate of location 2
    ----- Y coordinate of location 2
    ...
    112 - pointer to 1 --> this points to location 1. We'll call it "APointVariable"


    If we use
    set APointVariable = location2
    , you ONLY change the contents of the pointer, this is the variable at adress 112. So you do not change the contents of adress 1 or 3!

    ==>
    Adress --- Value
    ...
    112 - pointer to 3 --> this now points to location 2
    .
    Note that location 1 and location 2 are STILL the same locations, they have NOT changed. But if you now use "APointVariable", it will obviously work with the X and Y coordinates of location 2.

    However, if we'll never use location 2 again, we're wasting the memory used on adress 3. If we forget about this, we now have a memory leak. To avoid memory leaks, we can use functions such as
     call RemoveLocation(APointVariable)

    What happens now is: your computer will follow the pointer in APointVariable, which still points to adress 3. Then, it removes the contents of adress 3 so it can be used again.

    Our new situation is:
    Adress --- value

    1 --- X coordinate of location 1
    ----- Y coordinate of location 1
    3 --- NULL

    ...
    112 - pointer to 3 --> this points to location 2. Location 2 no longer exists, so if you do something with "APointVariable" at THIS moment, nothing will happen, simply because there IS no location variable where the pointer points to.

    This is why we need to remove locations with "RemoveLocation()".
    Ofcourse, it's a very simplistic view, in a real situation memory management is much more complex. One of the things that makes it more complex is the so-called "Reference counter".

    Whenever any new variable is created, a unique id is assigned to it. Your computer needs to be able to distinguish 2 variables of the same type, so we do this by adding an id to each variable. This id essentially is an "integer", unique for each variable.

    Each new variable ALSO has an additional variable called the "Reference counter". The idea behind the reference counter is that it counts the amount of pointers that point to this variable. If it reaches 0, it means nothing points to the variable anymore. When this happens, our variable is no longer used and in theory it could automatically be removed from the memory (so you no longer have to use "RemoveLocation" to remove a location from the memory).
    Blizzard forgot to implement this, so you still must use "RemoveLocation" to clear your memory.

    The reference counter is also used for something else. If it reaches zero, not only can the memory be removed, but this "unique" id can also be reused now by another variable! After all, the id is no longer in use now so another variable can use it! This is to avoid running out of unique id's if you keep creating new variables. I don't know what happens if you DO run out of unique id's, but it can't be good. Probably a crash.
    Blizzard DID implement this, so if the ref counter reaches zero, the id is free to be used by another variable now.
    Yet again, blizzard forgot to do something. You see, when you use "RemoveLocation" on a variable, you are removing the location from the memory. The unique ID and reference counter are still there though, and the reference counter is NOT zero yet, because the actual pointer STILL points to the location. It would be common sense if blizzard would automatically decrease the reference counter when the location is removed so the unique id is free to be used again, but they forgot to do so. So now aside from the fact we need to remove variables manually, we ALSO need to decrease the reference counter manually. We do so simply by letting our pointer point to "nothing". This is what happens when you say:
    Code (vJASS):
    set APointVariable = null

    The actual pointer (APointVariable itself) is still there, it still uses memory. However, it now points to "nothing". Lucky for us, the actual pointer does get automatically removed when it's no longer needed". This is in the case of local variables.

    Code (vJASS):

    call RemoveLocation(aLocationPointer) // remove the memory used by the location where aLocationPointer points to
    set aLocationPointer = null // we make aLocationPointer point to "nothing" now, so our reference counter is decreased and the unique id is freed.
     


    One important note: when a global pointer variable is used, the actual pointer variable will always be in the memory. After all, you will ALWAYS be able to use your global variable. The "actual location" behind this pointer might be removed from the memory, but since the pointer is global, the pointer itself is never removed. Thus, this means that there's no need to decrease the reference counter of global variables! Why? Simply because the pointer will always stay in memory.

    So, recapitulation:
    Code (vJASS):
    //for local variables:
    local location localpointer = GetSpellTargetLoc() // create the location
    call RemoveLocation(localpointer) // destroy the location
    set localpointer = null // set pointer to our destroyed location = nothing
    // ==> we need to set localpointer = null so the reference counter is decreased. localpointer is a local variable which is removed when it's no longer needed.

    // for global variables:
    call RemoveLocation(udg_globalpointer)
    // ==> we don't need to set udg_globalpointer = null, because it's a global variable, which is NEVER removed. If it isn't removed, why bother?
     


    p.s. wow, this post became longer than I thought it was going to be...
     
  13. mecatronic

    mecatronic

    Joined:
    Jun 20, 2005
    Messages:
    112
    Resources:
    1
    Maps:
    1
    Resources:
    1
    thanks for the best explanation about location leaks I've ever seen!
     
  14. busterkomo

    busterkomo

    Joined:
    Jun 17, 2007
    Messages:
    1,423
    Resources:
    1
    Tutorials:
    1
    Resources:
    1
    Ugh, you're teaching him the wrong way to do it. Since it's a global variable, you don't need to null. Only in the case of local variables, such as this:

    Code (vJASS):
    local location l = GetUnitLoc(GetTriggerUnit()
    //do something
    call RemoveLocation(l)
    set l = null
     
  15. Eleandor

    Eleandor

    Joined:
    Aug 21, 2005
    Messages:
    3,681
    Resources:
    2
    Models:
    1
    Tutorials:
    1
    Resources:
    2
    me? I've said that you don't need to null globals...
     
  16. Justify

    Justify

    Joined:
    Mar 16, 2008
    Messages:
    935
    Resources:
    5
    Models:
    1
    Icons:
    1
    Spells:
    3
    Resources:
    5
    I think he was talking with me.
    I just CnP his "code" and forgot to see that it was a global :O
     
  17. busterkomo

    busterkomo

    Joined:
    Jun 17, 2007
    Messages:
    1,423
    Resources:
    1
    Tutorials:
    1
    Resources:
    1
    Sorry about the confusion. I was talking about Justify.
     
  18. Super-Sheep

    Super-Sheep

    Joined:
    Oct 21, 2006
    Messages:
    2,823
    Resources:
    1
    Maps:
    1
    Resources:
    1
    • Unit Group - Pick every unit in (Random 10 units from UnitGroup1) and do (Actions)


    Would this leak twice?

    I mean that, are "Random 10 units" and "UnitGroup1" two different groups?
     
  19. Justify

    Justify

    Joined:
    Mar 16, 2008
    Messages:
    935
    Resources:
    5
    Models:
    1
    Icons:
    1
    Spells:
    3
    Resources:
    5
    Yes. Random 10 units is a new group.
     
  20. Super-Sheep

    Super-Sheep

    Joined:
    Oct 21, 2006
    Messages:
    2,823
    Resources:
    1
    Maps:
    1
    Resources:
    1
    I thought so too, but just wanted to be sure. Thanks.