Destroying and Removing Globals

Carl-Fredrik

New Member
Reaction score
51
Hi everyone...

So, earlier I've been doing GUI, and what I learned is "To prevent memory leaks you must destroy groups, locations, forces, etc.".

But now when I'm learning vJASS/JASS I notice that when I destroy global groups (created between "globals" and "endglobals") triggers will fuck up. So I was wondering, won't they leak if I let them exist through the game? :confused:

That's basically what I learned in GUI, if you create a variable you'll have to destroy it...

Example (GUI):

Trigger:
  • Bandit Camp
    • Events
      • Unit - A unit Is attacked
    • Conditions
      • Or - Any (Conditions) are true
        • Conditions
          • (Attacked unit) Equal to Assassin 0239 <gen>
          • (Attacked unit) Equal to Assassin 0238 <gen>
          • (Attacked unit) Equal to Bandit Lord 0253 <gen>
          • (Attacked unit) Equal to Tent 0229 <gen>
          • (Attacked unit) Equal to Rogue 0236 <gen>
          • (Attacked unit) Equal to Bandit 0235 <gen>
          • (Attacked unit) Equal to Bandit 0234 <gen>
          • (Attacked unit) Equal to Bandit 0237 <gen>
    • Actions
      • Set Temp_UnitGroup = (Units in Bandits <gen> owned by Neutral Passive)
      • Unit Group - Pick every unit in Temp_UnitGroup and do (Unit - Change ownership of (Picked unit) to Neutral Hostile and Change color)
      • Custom script: call DestroyGroup (udg_Temp_UnitGroup)
      • Set Temp_UnitGroup = (Units in Bandits <gen> owned by Neutral Hostile)
      • Unit Group - Order Temp_UnitGroup to Attack (Attacking unit)
      • Custom script: call DestroyGroup (udg_Temp_UnitGroup)


Here I set/destroy the group twice and it works perfectly.


Example (JASS/vJASS):

JASS:
scope MSCommand initializer InitMS

globals
    private group MSGROUP = CreateGroup()
endglobals

private function TypeComparisons takes nothing returns boolean
    local unit u = GetFilterUnit()
    return IsUnitType(u, UNIT_TYPE_HERO) == true and IsUnitAliveBJ(u) == true and GetOwningPlayer(u) == GetTriggerPlayer()
endfunction

private function DoThis takes nothing returns nothing
    call DisplayTextToForce( GetForceOfPlayer(GetTriggerPlayer()), ( "|c00ff8000Movement Speed: " + ( R2S(GetUnitMoveSpeed(GetEnumUnit())) + udg_Endtag ) ) )
endfunction

private function Actions takes nothing returns nothing
    call GroupEnumUnitsInRect(MSGROUP, GetPlayableMapRect(), Condition (function TypeComparisons))
    call ForGroup(MSGROUP, function DoThis)
    call GroupClear(MSGROUP)
    call AdjustPlayerStateBJ(-500, GetTriggerPlayer(), PLAYER_STATE_RESOURCE_GOLD)
endfunction

private function InitMS takes nothing returns nothing
    local trigger t = CreateTrigger()
    call TriggerRegisterPlayerChatEvent(t, Player(0), "-ms", true)
    call TriggerRegisterPlayerChatEvent(t, Player(1), "-ms", true)
    call TriggerRegisterPlayerChatEvent(t, Player(2), "-ms", true)
    call TriggerRegisterPlayerChatEvent(t, Player(3), "-ms", true)
    call TriggerRegisterPlayerChatEvent(t, Player(4), "-ms", true)
    call TriggerRegisterPlayerChatEvent(t, Player(5), "-ms", true)
    call TriggerRegisterPlayerChatEvent(t, Player(6), "-ms", true)
    call TriggerRegisterPlayerChatEvent(t, Player(7), "-ms", true)
    call TriggerRegisterPlayerChatEvent(t, Player(8), "-ms", true)
    call TriggerRegisterPlayerChatEvent(t, Player(9), "-ms", true)
    call TriggerAddAction(t, function Actions)
endfunction
endscope


If I destroy the group here this will only work once...


I was also wondering, does all local variables that I create need to be nulled?

Thanks in advance!

// Carl-Fredrik
 

Kenny

Back for now.
Reaction score
202
Globals are just allocated memory at the start of the game and will not "leak" if you leave them, so no, you don't need to null/destroy them. They can be left alone. Don't quote me on this, as I'm no expert. But they do not need to be cleared, I'm sure of that.

All local variables that create handles need to be nulled. So, unit, location, group, and all that need to be nulled, while real, integer, player variables do not.

Also you do not need to use GroupClear() when using GroupEnum functions, as those functions will clear the group before re-use.
 

Artificial

Without Intelligence
Reaction score
326
> I was wondering why it doesn't leak since obviously GUI groups does if you don't destroy them
The thing is that GUI things like (Units in Bandits <gen> owned by Neutral Passive) create a new group each time they're called. Every created handle needs to be destroyed when no longer used to avoid the memory leak. In this Jass script you only create the group when the globals are initialized (GroupEnum doesn't create a new group, it just adds the units to the given group), and use it throughout the game. Since its usage never ends, it shouldn't be destroyed.

If you would assing the value CreateGroup() to MSGROUP each time Actions is run, then you'd also need to call DestroyGroup(MSGROUP) when the Actions function ends, as that particular handle would no longer be used (a new one would be created when the function would run again).
 

saw792

Is known to say things. That is all.
Reaction score
280
In your GUI example you are creating a new group every time the trigger fires (that 'set' function actually creates the group). In your JASS example you create one group, once. That same group is used throughout the entire game. If you destroy that group of course it will stop working.

It isn't a leak if you need the group to exist for the functionality of the code. The GUI code loses the reference to each old group when a new one is created which is why you need to set it to a variable and destroy it.

Local variables that can be created and destroyed should be nulled. The player variable extends handle as well, but these are created once at the start of the game and never destroyed so they don't need to be nulled. Same with integers, reals, strings, booleans and other 'static' handles.

EDIT: Arti beat me
 

Carl-Fredrik

New Member
Reaction score
51
Ah! I wasn't aware that that "Set (Variable)" in GUI actually creates a new group each time it is used. That explains quite much :)

And thanks for the local-variable information as well!

+Rep to everybody who helped explaining of course ;)

Edit: Can't give you rep right now saw, I'll give ya later provided that I remember it.
 

Viikuna

No Marlo no game.
Reaction score
265
You can always convert that GUI stuff to Jass, press Ctrl + click some red GUI BJ function and check what it does ( Function List is just awesome. ) Some of them indeed leak.
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Members online

      No members online now.

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top