• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • It's time for the first HD Modeling Contest of 2024. Join the theme discussion for Hive's HD Modeling Contest #6! Click here to post your idea!

[JASS] Does this function avoid a handle leak?

Status
Not open for further replies.
It is known that if you don't null local handle variables at the end of a function, the reference counter to the handle is not updated. However, this is not the case for variables that are parameters for the function.

My question is, does this only apply if you don't change the value of the paramater variable, or does the reference counter get updated for the value the variable had when it went out of scope?

I know this seems like a pretty stupid question, but I just want to be 100% sure that this will work. The function is supposed to be called like this:

JASS:
    set YourUnit = GUDR_PlayerHasGeneratorSelected(YourPlayer, null)

Thanks for your time.

JASS:
function GUDR_PlayerHasGeneratorSelected takes player whichPlayer, unit firstOfGroup returns unit
    local group slctGrp = CreateGroup()
   
    call GroupEnumUnitsSelected(slctGrp, whichPlayer, null)
    set firstOfGroup = FirstOfGroup(slctGrp)
   
    call DestroyGroup(slctGrp)
    set slctGrp = null
   
    if HaveSavedHandle(udg_GUDR_Hashtable, GetHandleId(firstOfGroup), 0) then
        return firstOfGroup
    endif
   
    return null
endfunction
 
It should not do any difference. I believe it doesn't leak, or let's say leaks likewise, as if you don't change the value. Or handle is cleaned, or it isn't, but it should not work like it's going from value to reference.

Let me note it's very bad practice to work with parameters to replace locals. A good alternative would be here to work with a global variable, if an agent needs to be returned. You could, or create a own global for it ( I would probably do this), or many people also use globals already provided by blizzard, like bj_lastCreatedUnit for units. But be aware, that if you use blizzard globals, that you might not think of this variable getting overwritten in a possible other scenario.
 
I can't create any globals myself since this is supposed to be a JASS function, not vJASS =/

I had forgotten about the bj globals though, I will probably use bj_groupRandomCurrentPick. That should be much better. Thanks!

EDIT: Actually, I could create an udg_ variable since it's a unit. Still, I would rather avoid adding a global that would only be used for this purpose.
 
Last edited:
Status
Not open for further replies.
Top