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 haven't received your rank award? Then please contact the administration.
    Dismiss Notice
  3. Lead your forces to battle in the 15th Techtree Contest. The call is yours, commander!
    Dismiss Notice
  4. The reforging of the races is complete. Come see the 14th Techtree Contest Results.
    Dismiss Notice
  5. It's time to choose your horse in the race - the 32nd Modeling Contest Poll is up!
    Dismiss Notice
  6. 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.

Memory Leaks

Discussion in 'Trigger (GUI) Editor Tutorials' started by IcemanBo, Mar 6, 2015.

  1. IcemanBo

    IcemanBo

    Joined:
    Sep 6, 2013
    Messages:
    6,378
    Resources:
    22
    Maps:
    3
    Spells:
    11
    Template:
    1
    Tutorials:
    4
    JASS:
    3
    Resources:
    22
    Technically seen, yes a group does already exist, and overwriting it does cause a leak.
    I've seen people destroying them in the spells init trigger for GroupArray[0] and [1].
    It won't hurt noone, because one-fired leaks are like nothing, but it can be noted unter Miscs.
     
  2. KILLCIDE

    KILLCIDE

    Administrator

    Joined:
    Jul 22, 2015
    Messages:
    3,502
    Resources:
    20
    Models:
    2
    Icons:
    10
    Spells:
    7
    Tutorials:
    1
    Resources:
    20
    I have been living my life wrong this whole time ;_;
     
  3. Rasler

    Rasler

    Joined:
    Jan 15, 2016
    Messages:
    137
    Resources:
    0
    Resources:
    0
    i HAVE a few questions. In my new project i use transitions map, where it "changes" map constantly (not loads). When it changes, it means that the map is starting with 0 memory leaks? its like restarting and the memory used is destroyed?
     
  4. Rasler

    Rasler

    Joined:
    Jan 15, 2016
    Messages:
    137
    Resources:
    0
    Resources:
    0
    Areas?, i say that are totally diferent maps. Like rexxars campaign. But changing maps instead of loading them..
     
  5. PurgeandFire

    PurgeandFire

    Code Moderator

    Joined:
    Nov 11, 2006
    Messages:
    7,426
    Resources:
    18
    Icons:
    1
    Spells:
    4
    Tutorials:
    9
    JASS:
    4
    Resources:
    18
    If it is like Rexxar's campaign, then yes all the previous map's memory will be cleared before loading the next map. So it is kinda like starting fresh.
     
  6. IcemanBo

    IcemanBo

    Joined:
    Sep 6, 2013
    Messages:
    6,378
    Resources:
    22
    Maps:
    3
    Spells:
    11
    Template:
    1
    Tutorials:
    4
    JASS:
    3
    Resources:
    22
    Updated slightly and also added "Technique of removal" to chapter "Refrerence Leaks",
    because I still see sometimes that it is confusing people.

    Edit Udate again.

    Btw GhostHunter123, sorry I forgot to answer you.

    If a variable is initialisized already it does point to an object.
    So when you overwrite an already initialisized variable, it will lead to leak.
    Non-arrays group variables are initialisized.
    Array group variablesa are initialisized with index [0] and [2].
     
    Last edited: Jan 30, 2016
  7. Aniki

    Aniki

    Joined:
    Nov 7, 2014
    Messages:
    536
    Resources:
    6
    Tools:
    1
    Maps:
    1
    Spells:
    1
    JASS:
    3
    Resources:
    6
    It's probably worth mentioning that functions that return handles should probably not use a local variable in the return statement
    because that would prevent the handle-id of ever getting recycled by the game: (source),
    that thread is a bit heated but very fun to read =)

    example:

    handle-id leak:
    Code (vJASS):

    function CreateTransparentFootman takes player p returns unit
        local unit u = CreateUnit(p, 'hfoo', 0, 0, 0)
        call SetUnitVertexColor(u, 255, 255, 255, 96)
        return u
    endfunction
     


    no handle-id leak:
    Code (vJASS):

    globals
        unit footman
    endglobals

    function CreateTransparentFootman takes player p returns unit
        local unit u = CreateUnit(p, 'hfoo', 0, 0, 0)
        call SetUnitVertexColor(u, 255, 255, 255, 96)
        set footman = u
        set u = null
        return footman

        // or avoiding locals all together
        // set footman = CreateUnit(p, 'hfoo', 0, 0, 0)
        // call SetUnitVertexColor(footman, 255, 255, 255, 96)
        // return footman
    endfunction

    // the last X'd vars from Blizzard.j could be used (eg.: bj_lastCreatedUnit) I guess, but those are for GUI and there are
    // some handle types that are missing (eg.: region), so it's probably better to declare dedicated globals for those functions that return handles
     


    And of course there's no point in nulling handles if they are never destroyed, i.e they are always recycled (timers, units, groups, etc.).
     
  8. IcemanBo

    IcemanBo

    Joined:
    Sep 6, 2013
    Messages:
    6,378
    Resources:
    22
    Maps:
    3
    Spells:
    11
    Template:
    1
    Tutorials:
    4
    JASS:
    3
    Resources:
    22
    Yeah, thanks.
    Will think of where to add a part about it.

    The solution is kind of ugly, that's why some just prefer taking the small (4 bytes?) leak..
    but it might be mentioned, you're very right.
     
  9. BPower

    BPower

    Joined:
    Mar 18, 2012
    Messages:
    1,709
    Resources:
    21
    Spells:
    15
    Tutorials:
    1
    JASS:
    5
    Resources:
    21
    Go with
    bj_lastCreatedUnit
    as global unit variable.
     
  10. IcemanBo

    IcemanBo

    Joined:
    Sep 6, 2013
    Messages:
    6,378
    Resources:
    22
    Maps:
    3
    Spells:
    11
    Template:
    1
    Tutorials:
    4
    JASS:
    3
    Resources:
    22
    Added it as "ExtraNote" in "Reference Leaks" -> "Technique of removel"
     
  11. Skyflash

    Skyflash

    Joined:
    Mar 6, 2015
    Messages:
    129
    Resources:
    0
    Resources:
    0
    Dear IcemanBo
    can you update your post and mention these leaks which is Increasing Handle counts?

    1-Destructible Leak
    • Destructible - Pick every destructible within Radius of Point and do (Actions)
      • Loop - Actions


    Solution
    • Custom script:set Rect= GetRectFromCircleBJ(Point,Radius)
    • Destructible - Pick every destructible in Rect and do (Actions)
      • Loop - Actions


    Reason
    Code (vJASS):
    function EnumDestructablesInCircleBJ takes real radius, location loc, code actionFunc returns nothing
        local rect r

        if (radius >= 0) then
            set bj_enumDestructableCenter = loc
            set bj_enumDestructableRadius = radius
            set r = GetRectFromCircleBJ(loc, radius)
            call EnumDestructablesInRect(r, filterEnumDestructablesInCircleBJ, actionFunc)
            call RemoveRect(r)
     //they have forgotten to Null local rect +1 Handle Leaks
        endif
    endfunction


    2-Most Common Leak that we are seeing these days Unit-Pick in Range

    • Unit Group - Pick every unit in (Units within Radius of Point) and do (Actions)
      • Loop - Actions


    Solution
    • Custom script: set udg_group=CreateGroup()
    • Custom script: call GroupEnumUnitsInRange(group, GetLocationX(Point), GetLocationY(Point), Radius, null)
    • Unit Group - Pick every unit in (Units in Rect) and do (Actions)
      • Loop - Actions
    • Custom script:call DestroyGroup(udg_group)



    Reason
    Code (vJASS):

    function GetUnitsInRangeOfLocMatching takes real radius, location whichLocation, boolexpr filter returns group
        local group g = CreateGroup()
        call GroupEnumUnitsInRangeOfLoc(g, whichLocation, radius, filter)
        call DestroyBoolExpr(filter)
        return g
    endfunction
    // they forgot to remove Group and null the Global g +2 Handle Leaks
     
     
    Last edited: Mar 16, 2016
  12. IcemanBo

    IcemanBo

    Joined:
    Sep 6, 2013
    Messages:
    6,378
    Resources:
    22
    Maps:
    3
    Spells:
    11
    Template:
    1
    Tutorials:
    4
    JASS:
    3
    Resources:
    22
    Hey. Thanks for posting!

    First there is to mention that you are right, that many BJs don't null local agents.
    But for GUI usage it's pretty negligible, and mods probably won't ever enforce the user to change it. (GUI)

    But there is also to say, that not the handle per se leaks, but it's assigned id. link,
    so for example this statement was not very correct:
    ..., because it returns the group handle, and does not (handle-) leak.

    I actually named some functions in "Things that always leak", but I did not finish it, because there are just too many of them I think.

    What I can do is to write a all for one warning when using BJs, that they might leak.
    But as long as they are not known for special bugs, like the "Camera - Pan camera as necessary...", they're mostly accepted for GUI users.

    What do you say?
    If you want to discuss further, please do!
     
  13. Skyflash

    Skyflash

    Joined:
    Mar 6, 2015
    Messages:
    129
    Resources:
    0
    Resources:
    0
    Yes you are right but as a user who don't likes hard codes I would prefer to have clean code even if I had to use jass little bit but actually its too bad to see these leaks on our approved resources because maybe no one has noticed these leaks before or they are just getting ignored
     
    Last edited: Mar 17, 2016
  14. IcemanBo

    IcemanBo

    Joined:
    Sep 6, 2013
    Messages:
    6,378
    Resources:
    22
    Maps:
    3
    Spells:
    11
    Template:
    1
    Tutorials:
    4
    JASS:
    3
    Resources:
    22
    You're of course always free to your own ways for writing code.
    I don't know if all GUI user will re-write the BJ functions, though, to avoid a reference leak.
    If one can easily avoid it, then yes, but if not, then myself would probably not enforce it.

    Addes this for now at "Things that always leak".

     
  15. Arad MNK

    Arad MNK

    Joined:
    Dec 11, 2014
    Messages:
    1,887
    Resources:
    3
    Maps:
    2
    Spells:
    1
    Resources:
    3
    Add this again :D

    Under TESH, "Function list..." ;p

    Or opening Blizzard/Common.j .
     
  16. Flux

    Flux

    Joined:
    Feb 6, 2014
    Messages:
    2,333
    Resources:
    28
    Maps:
    1
    Spells:
    19
    Tutorials:
    2
    JASS:
    6
    Resources:
    28
    0.04 kilobits? or you meant to write 0.04 kilobytes (0.04 KB)?
     
  17. IcemanBo

    IcemanBo

    Joined:
    Sep 6, 2013
    Messages:
    6,378
    Resources:
    22
    Maps:
    3
    Spells:
    11
    Template:
    1
    Tutorials:
    4
    JASS:
    3
    Resources:
    22
    I think kilobytes.
     
  18. Avahor

    Avahor

    Joined:
    Sep 29, 2008
    Messages:
    287
    Resources:
    8
    Skins:
    4
    Maps:
    4
    Resources:
    8
    hi mate:
    There's a sintax error on reference leaks -> Technique of removal -> Extra notes -> good Example

    Code (vJASS):
    function someUnitReturn
        bj_lastCreatedUnit = CreateUnit( ... )
        return u
        // Hooray! We still have access to the global variable :)
    endfunction


    var "u" isn't defined. that would cause some misunderstanding on readers.
    Edit: very good and needed tutorial
     
  19. Flux

    Flux

    Joined:
    Feb 6, 2014
    Messages:
    2,333
    Resources:
    28
    Maps:
    1
    Spells:
    19
    Tutorials:
    2
    JASS:
    6
    Resources:
    28
    Furthermore, shouldn't it return the global not the local? Also, you mispelled
    local
    , under "Technique of Removal"
     
  20. IcemanBo

    IcemanBo

    Joined:
    Sep 6, 2013
    Messages:
    6,378
    Resources:
    22
    Maps:
    3
    Spells:
    11
    Template:
    1
    Tutorials:
    4
    JASS:
    3
    Resources:
    22
    Oh, right!
    It should be
    return bj_lastCreatedUnit
    . Changed it.

    Thanks :)

    Lol, three times. Copy & paste ftw. :D

    Fixed. Thanks, guys.