Tutorial How to use Groups without leaking

Jesus4Lyf

Good Idea™
Reaction score
397
>This is especially useful as GroupEnum will cause a group to leak even if it is destroyed, so simply using one global group will create only one leak (am I correct in that the leak is only one per group, Jesus4Lyf?).

Yeah. Once a group has an enum called for it, destroying it will leak (as per this tut). It seems that each enum overwrites the data from the previous one that would otherwise leak, or something. :)

I didn't look too deeply as to whether the size of the leak is difference based on how many enums you called, I found the point simply to be that you should recycle groups...

JASS:
function CopyGroup takes group g returns group //This function was NOT written by me, I've got it from a tutorial on wc3c.net
    set bj_groupAddGroupDest = CreateGroup()
    call ForGroup(g, function GroupAddGroupEnum)
    return bj_groupAddGroupDest
endfunction

function CountLivingHeroesOfForce takes force team returns integer
    local integer count = 0
    local unit u
    local group g
    set g = CopyGroup(Heroes)
    loop
        set u = FirstOfGroup(g)
        exitwhen u == null 
        if IsPlayerInForce(GetOwningPlayer(u), team) and GetWidgetLife(u) > 0.045 then
            set count = count + 1
        endif
        call GroupRemoveUnit(g, u)
    endloop
    call DestroyGroup(g)
    return count
endfunction

-->
JASS:
scope CountLivingHeroesOfForceScope
    globals
        private integer Count
        private force Team
        private unit Enum
    endglobals
    private function CountLivingHeroesOfForceEnum takes nothing returns nothing
        set Enum=GetEnumUnit() // Could be a local, but you'd need to null it then.
        if IsPlayerInForce(GetOwningPlayer(Enum),Team) and GetWidgetLife(Enum)>.405 then // You'd put 0.045.
            set Count=Count+1
        endif
    endfunction
    function CountLivingHeroesOfForce takes force team returns integer
        set Count=0
        set Team=team
        call ForGroup(Heroes,function CountLivingHeroesOfForceEnum)
        return Count
    endfunction
endscope

(I have not syntax checked it.)

>Could somebody explain to me why looping trough a group with FirstOfGroup(group) is deprecated?
It is deprecated in different circumstances for different reasons. In this situation, it is deprecated due to efficiency. The tutorial explains that in other situations, ghost references can cause a FirstOfGroup loop to terminate early (if the group was not created immediately before the FoG loop occurred).

>It should just be as simple as stating that if Create/Destroy group functions are used, that the normal handle nulling rules still apply.
Hm, I'll add that at some point I guess.
 

Deaod

Member
Reaction score
6
Just to stir things a bit up: Safety shouldnt be debug only. Imagine you miss a (critical) bug while testing. Imagine the bugged version would be released. Imagine the bug occurs once every ten games and kills the whole game (for whatever reason).

Also, this was long known and one of the prime reasons why something like GroupUtils exists.
Reinventing the wheel isnt exactly hard, is it?
The size of the leak is not related to the number of times you call an Enum. Either the group or the unit has to change. Else, recycling would be pretty senseless, wouldnt it?
Also note that this bug has nothing to do with handle ids. Different units with the same handle id leak. The same applies to groups.
 

Prozix

New Member
Reaction score
7
Thanks for pointing those things out for me Darthfett and Jesus4lyf.
Captain griffin said:
GroupRefresh does not affect the group in a noticeable way, aside from removing the shadow references in the hash table, which speeds up most operations (see the link above for further details). The only effect it will have on outputs is on FirstOfGroup, which can return null when it comes to a shadow reference in the list, even if there are real units after it. GroupRefresh clears those out, so makes FirstOfGroup act properly again.
I found this in the GroupUtils thread on Wc3C and that's another reason why you should avoid using FirstOfGroup, it can produce unexpected results if your loop ends before it should. Giving 3/12 of the heroes in the game a huge stat bonus would be somewhat unfair I guess.
 

Jesus4Lyf

Good Idea™
Reaction score
397
>Giving 3/12 of the heroes in the game a huge stat bonus would be somewhat unfair I guess.
And if you read this tutorial, you'd know why that's not possible (unless you have temporary heroes in the game).

>Just to stir things a bit up: Safety shouldnt be debug only.
Wrong thread, that's for Recycle. :( Off topic.

>The size of the leak is not related to the number of times you call an Enum. Either the group or the unit has to change.
>Also note that this bug has nothing to do with handle ids. Different units with the same handle id leak. The same applies to groups.

Good information, but I'd have to test it before I include it. It is probably too detailed for this tutorial - unlses you are saying that using different units in enums actually does cause a leak, regardless (because even though the GROUP is not changing, the unit being enummed is - seems to be what you've said).

Then what? Deprecate enums?
 

Deaod

Member
Reaction score
6
unlses you are saying that using different units in enums actually does cause a leak, regardless
Thats what my tests indicated.


Then what? Deprecate enums?
No, those are leaks you cant prevent from happening. You can minimize the leaking through recycling, but even then you still leak a bit. It should be no big deal.
 

Viikuna

No Marlo no game.
Reaction score
265
One more reason for people to do all their group enums with only one group. ( other than that it is the easiest way anyways... )

edit. and maybe you should also recycle all your units, lols.
 

Prozix

New Member
Reaction score
7
>And if you read this tutorial, you'd know why that's not possible (unless you have temporary heroes in the game).
A game where you delete the heroes if a user has left could cause that problem right?
 

Jesus4Lyf

Good Idea™
Reaction score
397
>No, those are leaks you cant prevent from happening.
That's not true. You could use an AIDS struct to form a linked list of every unit in the game, and use that for enumerations. Could be faster than the native enums, you know.

>edit. and maybe you should also recycle all your units, lols.
Or this.

>A game where you delete the heroes if a user has left could cause that problem right?
Yes.
 

Weep

Godspeed to the sound of the pounding
Reaction score
401
1.24c on Westfall said:
- Fixed a minor memory leak when using GroupEnum natives.
Is this the create-enum-destroy leak?
 

Narks

Vastly intelligent whale-like being from the stars
Reaction score
90
WTF?!
ARE BLIZZARD FIXING THINGS?!!

Code:
--------------------------------------------------------------------------
Patch 1.24c
--------------------------------------------------------------------------

BUG FIXES
- Fixed a Bug that disabled Spirit Walkers from resurrecting other Spirit 
  Walkers.
- JASS has been disabled.

MAPS

- (6)WizardsRetreat
   - separated two creep camps near both corner starting locations to
     prevent them from calling each other for help when one is attacked.  

- (6)Gnollwood
   - separated two creep camps near merchant buildings to prevent them from 
     calling each other for help when one is attacked.  

- (6)BlizzardTD 
   - Fixed phase-shifting units to behave normally when attacked. 
 
- (12)WormWar 
   - Fixed a bug that causes player worms to become immobile after using
     powerups while respawning.
 

SanKakU

Member
Reaction score
21
i tried changing DoThings but it's not working. i get some bugged error message about my start location (is jasshelper ever going to fix that?)

are you not able to do those things that i'm trying to do?

edit: ah nevermind i have no idea if the problem is with the dothings function...what i want to do is basically something like when a sorceress dies, all the sorceress' owner's scout towers, ziggurats, and watchtowers are destroyed. something like that. what's the best way to do it?


private function DoThings takes nothing returns boolean
if GetUnitTypeId(GetFilterUnit()) == 'opgf' then
call RemoveUnit(GetFilterUnit())
elseif GetUnitTypeId(GetFilterUnit()) == 'ofor' then
call RemoveUnit(GetFilterUnit())
elseif GetUnitTypeId(GetFilterUnit()) 'uzg2' then
call RemoveUnit(GetFilterUnit())
elseif GetUnitTypeId(GetFilterUnit()) 'uzig' then
call RemoveUnit(GetFilterUnit())
elseif GetUnitTypeId(GetFilterUnit()) 'hatw' then
call RemoveUnit(GetFilterUnit())
elseif GetUnitTypeId(GetFilterUnit()) 'hwtw' then
call RemoveUnit(GetFilterUnit())
endif
return false
endfunction
 

mapguy

New Member
Reaction score
46
TO prevent leaks, instead of creating my groups can I do this:

cause dummygroupunit to damage area with 0 damage in 300 radius

Code:
a unit takes damage

damage source equal to my dummygroupunit

add triggering unit to Group_g
//do stuff//
clear all units from group_g
 

Komaqtion

You can change this now in User CP.
Reaction score
469
No...

That will still use a Group Enumeration, and I've also heard it can cause massive problems to Mac users if you use that :S
 

Ayanami

칼리
Reaction score
288
TO prevent leaks, instead of creating my groups can I do this:



Code:
a unit takes damage

damage source equal to my dummygroupunit

add triggering unit to Group_g
//do stuff//
clear all units from group_g

What you can do is use Nestharus's Recycle, which is posted in the first post. Though, you'll have to give up your dreams of "Code everything in GUI", because I gave up that dream long time ago too.
 
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