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.

Question: unit and location variable leaks

Discussion in 'Triggers & Scripts' started by Xonatos, Nov 9, 2019.

  1. Xonatos

    Xonatos

    Joined:
    Nov 9, 2019
    Messages:
    1
    Resources:
    0
    Resources:
    0
    Question about unit and location variable leaks when they are in a loop...see partial code below:

    Code (vJASS):

    function example takes nothing returns nothing

    local group g = CreateGroup()
    local unit u
    local location l

    //code here to add units to group, omitted for clarity

    loop
        set u = FirstOfGroup(g)
        exitwhen u == null
        set l = //some location, different each time the loop runs

        //code to have unit do something with location

        call GroupRemoveUnit(g, u)
        //Do I need to null the unit and/or location variable here to prevent from leaking on the next iteration of the loop?
        //Or is it only necessary to do that at the end of the function?
    endloop

    call GroupClear(g)
    call DestroyGroup(g)
    set g = null

    call RemoveLocation(l)
    set u = null

    endfunction
     


    Where should the unit variable and location variable be nulled to prevent this function from leaking? Within the loop or at end of function?
     
  2. Pyrogasm

    Pyrogasm

    Joined:
    Feb 27, 2007
    Messages:
    3,056
    Resources:
    1
    Spells:
    1
    Resources:
    1
    You need to destroy the locations when they are no longer needed. So that would be inside the loop. If you do not destroy them the variable l will be overwritten and you’ll lose reference to the location, permanently leaking the data.

    Nulling local variables needs to happen at the end of a function (before a return statement). If you do not null them the handle id used for that handle stays in use permanently. The local variable can only be accessed/modified in the function it was declared, so it leaks the reference if not nulled. Globals do not need to be nulled.

    In this instance l needs to be nulled but u does not because, by the loop exitwhen logic, u is already null at the end of the function.
     
  3. Dr Super Good

    Dr Super Good

    Spell Reviewer

    Joined:
    Jan 18, 2005
    Messages:
    25,596
    Resources:
    3
    Maps:
    1
    Spells:
    2
    Resources:
    3
    Just before the function returns. If the only return is at the end of the function then there. If there are explicit function returns in the middle then just before those are executed. This only applies to JASS, since Lua does not suffer from this.