| 10-28-2004, 08:51 PM | #1 |
So I was all nice and secure inside my nice little bubble of comfort. Then I read something here, and see that you have to set local handle variables to null at the end of a function to avoid a memory leak. Now I'm all confused. I had thought that there was no reference counter in JASS and it didn't matter if you set a variable to null or not, but this seems to contradict that. So, how does this work, exactly? And what happens if you want to return a handle variable? There's no time to set it to null after it's returned... |
| 10-28-2004, 08:58 PM | #2 |
Use a global variable, IT IS the only way, local unit u=dsagsd udg_globalunit = u set u=null return udg_globalunit |
| 10-28-2004, 09:22 PM | #3 |
We talked about that a lot in old threads. The conclusion in the end was the following guess (in short): For each object you create, there is an intermediate handle object that among other things serves as a tombstone. For this object there is a reference counter and it is buggy in the case of local variables (although not parameters). At the end of the life of the local variables the reference counter is not reduced. |
| 10-28-2004, 10:43 PM | #4 |
so, wait.. does removing them do? or do i have to also set them to null to get rid of lag, it sounds like i do... |
| 10-29-2004, 02:07 AM | #5 | |
Quote:
And what about returning handle values? Will those always leak unless you use a global like Lord Vexorian just said? |
| 10-29-2004, 02:58 AM | #6 |
no, because you can actually set the handle to a local variable like local handle LOCALVAR = [function that returns handle of LOCALVAR's type] can't you? im not really good at the whole handle memory leak thing. |
| 10-29-2004, 12:29 PM | #7 |
Yes, pointer to pointer but only the tombstone has a reference counter. The object the tombstone points to always has to be destroyed manually (that is the main source of memory leaks). So not setting local handles to null will only lose the tombstone which does not leak that much memory. Only globals help to totally avoid that. |
| 10-29-2004, 04:51 PM | #8 | |
Quote:
Code:
function GetAHandle takes nothing returns handle
local handle someHandle
set someHandle = [i]<get a handle somehow>[/i]
return someHandle
endfunction
function SomeOtherFunction takes nothing returns nothing
local handle someHandle
set someHandle = GetAHandle()
[i]<do something with someHandle>[/i]
set someHandle = null
endfunction |
| 10-29-2004, 11:36 PM | #9 |
GetAHandle will likely leak a tombstone. Since only one tombstone can be leaked per object, SomeOtherFunction does not add any additional leak. If GetAHandle did not leak the tombstone, then SomeOtherFunction would be fine. Directly passing a returned value on will not leak, only assigning it to a local that still has it at the end of the function. |
| 10-30-2004, 01:28 AM | #10 |
All right. So basically, if there's a non-null handle variable at the end of the function, there'll be a leak; regardless of if that value is returned. I think I get it. :) |
