HomeUser Control Panel (unavailable in archive)ForumsTutorialsArt GalleryResourcesMaps

Returns local groups?

09-17-2007, 11:53 PM#1
MrApples
How do you stop a group from leaking if you have to return it? In local form.

For example... g is a local group variable
Code:
return g 

The trigger ends there, yet g must still exist. Or will returning g pass the same exact value which can be destroyed later?
09-18-2007, 12:54 AM#2
Ammorth
you can use a global, or a variant of a return bug to convert the group to an integer, and then return the integer as a group. I recommend the first option over the second.
09-18-2007, 01:19 AM#3
MrApples
So it will leak? How will using globals change that? Without leaving the function of course...

How about WantDestroyGroup?
09-18-2007, 02:00 AM#4
PipeDream
Pass the group as an argument to the function and don't return it. Think GroupEnum.
09-18-2007, 02:01 AM#5
TaintedReality
Nononono. Ammorth misunderstood. The part that is leaking is not the pointer, but the group. The same old rule still applies - you need to destroy the group when you're done with it. If that's in a different function than the one the group was created in, so be it. Oh, just now read the last part of your post. The answer to your last question would be yes.

Edit: Pipe, what's wrong with returning it? He's still going to have to destroy it either way.
09-18-2007, 05:43 AM#6
iNfraNe
returning it leaks the pointer.
09-18-2007, 06:15 AM#7
Ammorth
I assumed he meant the variable g was leaking, not the actual group, since he was returning the group, which would mean he would be using the group in the calling function, hence not losing it.

What pipe means is doing something like:

Collapse JASS:
function GroupFunc takes group g returns nothing
    //Do stuff with g
endfunction

function YourFunc takes nothing returns nothing
    local group g = CreateGroup()
    call GroupFunc(g)
    //Do stuff with g
    call DestroyGroup(g)
    set g = null
endfunction

The engine will clean arguments of a function, so passing a handle will clean itself. Of course, you could implement this multiple ways, this is only an example.
09-18-2007, 04:59 PM#8
TaintedReality
I always learned that if you returned a variable, then war3 cleaned up its pointer, so you don't have to null it. Is this an exception to the rule or am I wrong?
09-18-2007, 05:05 PM#9
botanic
as a general rule wc3 doesn't do ANYTHING smart like that

returning just means that the pointer have moved you still need to null it in the returned function
09-18-2007, 05:53 PM#10
TaintedReality
Quote:
returning just means that the pointer have moved you still need to null it in the returned function

Local pointers (variables in war3) only exist within the function they were created in. The actual data being pointed to can be retrieved in another function, but not the actual pointer. So uhhh I'm going to have to disagree with you on this one :).
09-18-2007, 06:37 PM#11
botanic
ok fine :/ returning just means that the pointer VALUE has moved you still need to null it in the returned function

forgot a word :/
09-18-2007, 07:19 PM#12
Alexander244
Quote:
Originally Posted by TaintedReality
I always learned that if you returned a variable, then war3 cleaned up its pointer, so you don't have to null it. Is this an exception to the rule or am I wrong?

Taken from Pipedreams tutorial on Local Var Leaks:
Quote:
Originally Posted by PipeDream
The handle indexes an array of structures. Each structure contains a pointer to the unit, location or other object along with a reference count. Whenever you assign a handle to a variable, warcraft finds the structure and increments the reference count. When you reassign that variable, the count is decremented. In theory, when there are no more local or global variables with that small piece of data's address and the object it points to has been removed, it should deallocate itself. Unfortunately, blizzard never finished this functionality. Even worse, they left a bug in: When a variable goes out of scope, that is, when a function that had local variables returns, the reference does not decrement! If you try to destroy or remove a handle, that small piece of data will not be deallocated until the reference count drops to zero. This will never happen if you don't work around that bug.
09-18-2007, 07:24 PM#13
botanic
like I said wc3 doesn't ever do anything smart :/
09-18-2007, 07:34 PM#14
TaintedReality
Thanks Alex, I'm surprised that I've never known that. Although I guess there aren't really many times when not knowing that would have hurt me.
10-08-2007, 02:39 AM#15
MrApples
So I can't return a group without leaking?

I'm making a script called 'GetUnitsOfRelation', it's meant to be standalone. WantDestroyGroup would not work?