• 🏆 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] Correct group recycling

Status
Not open for further replies.

Kazeon

Hosted Project: EC
Level 33
Joined
Oct 12, 2011
Messages
3,449
I wonder am I recycling unit group using dynamic indexing correctly or not.. Please check..

JASS:
call GroupClear(Group[i])
set Group[i] = Group[MaxIndex]
set Group[MaxIndex] = null
call DestroyGroup(Group[MaxIndex])

I'm destroying them because I always create new group on cast.. please, tell me if I'm doing it wrong.. thanks.. :wink:

EDIT:
I think I'm doing it wrong.. how about this? which is correct?

JASS:
call DestroyGroup(Group[i])
set Group[i] = Group[MaxIndex]
set Group[MaxIndex] = null
call DestroyGroup(Group[MaxIndex])
 
The second one is almost correct. You don't need the last line though. That will just attempt to destroy a "null" group (which is useless):
JASS:
call DestroyGroup(Group[i])
set Group[i] = Group[MaxIndex]
set Group[MaxIndex] = null

Although, that isn't actually "recycling" a group. It is just recycling an index--the group will end up destroyed. "Recycling" groups means to keep a stack of groups undestroyed, and fetch/use them when you need it (recycle them when you're done).

Also, the "correctness" of it may depend on the rest of the code (such as when you decrement the index "i", etc.). But in general, the code above should do it.
 

Kazeon

Hosted Project: EC
Level 33
Joined
Oct 12, 2011
Messages
3,449
Recycling and destroy hmm, miss i something ?

I'm not saying you shoudn't destroy groups, quite the opposite in fact, i don't believe that much in group recycling. (efficiency and memory used)

I think I had a misunderstanding about recycling, as pnf said, it's not recycling the group, it's recycling the index. :)
 
Level 29
Joined
Oct 24, 2012
Messages
6,543
Am I missing something or? Shouldn't you null first the Group and then set it to Group[MaxIndex]? Aren't you overwriting it?


nulling a variable only stops it from pointing to an object in memory.
Since setting a variable to some other object is what is being done then it does not cause any leaks.

The only variables you have to null are locals. But it has been said that nulling globals can help. They still do not cause a leak when they are not nulled.
 
Level 17
Joined
Apr 27, 2008
Messages
2,455
Not nulling globals will leak some memory anyway.

I suppose you mean if it's never overwritten and holds an handle reference which is destroyed, yes, but that was not my point.

That is interesting I have not seen that one before.

Technically you also don't have to null function arguments (takes ...), they are not concerned by the reference counter bug (which can cause an handle id leak).
Also some handles don't have to be nulled (the ones which extends handle but not agent), at least that's the case for image and texttag, there is no reference counter for them. (so yes you can exploit that for some silly stuff)
 
Status
Not open for further replies.
Top