HomeUser Control Panel (unavailable in archive)ForumsTutorialsArt GalleryResourcesMaps

General Question about Leaks

06-01-2009, 08:56 AM#1
Cheezeman
Hey dudes!
So we all know that PolarProjectionBJ() is evil, because it leaks a location, right?
So I was thinking "If PolarProjectionBJ() leaks a location, shouldn't all functions that returns a handle leak a handle aswell?"
I've been tought to set handles = null when I'm done, but when using per example GetTriggerUnit() (which is a very bad example since I do not know how it works) is returns a unit which the function does not nullify (I think...)

So... why don't functions that returns a handle leak, while functions that returns a location leak?
06-01-2009, 10:24 AM#2
Anitarf
PolarProjectionBJ doesn't leak on it's own. It just sucks.
06-01-2009, 04:02 PM#3
MaD[Lion]
"So... why don't functions that returns a handle leak, while functions that returns a location leak?"

-locations are handles.
-Functions tat return handle doesnt leak, only if you make it so, the same with location (obvious).
Go google about memory leaking and youll know what it is and how to prevent
06-01-2009, 05:35 PM#4
Cheezeman
Quote:
Originally Posted by Anitarf
PolarProjectionBJ doesn't leak on it's own. It just sucks.
Tell me why please.
06-01-2009, 05:50 PM#5
grim001
Well you've noticed something that's correct...

Collapse JASS:
function MyFunction takes nothing returns unit
    local unit u = CreateUnit(...)
    //do other stuff to the unit
    return u //you can't null the unit if you return it, so it leaks
endfunction

If you want to prevent this from happening, you do it like this:
Collapse JASS:
function MyFunction_Sub takes unit u returns unit
    set u = CreateUnit(...)
    //do other stuff to the unit
    //because u is a parameter of the function this time, it won't leak
    return u
endfunction

function MyFunction takes nothing returns unit
    return MyFunction_Sub(null) //inlinable so there's no speed loss
endfunction
06-01-2009, 06:03 PM#6
Viikuna-
I didnt know that. Thats cool, and really nice and smooth actually.
06-01-2009, 06:19 PM#7
ploks
Can you use a temporary global?


Collapse JASS:
globals
private unit temp
endglobals

function A takes nothing returns unit
local unit u = CreateUnit()

//Do things
set temp = u
set u = null
return temp
endfunction
06-01-2009, 06:29 PM#8
Viikuna-
Yes you can, it works too. ( Thats how I used to do it )
06-01-2009, 06:40 PM#9
akolyt0r
wtf no need to do this
06-01-2009, 06:40 PM#10
0zyx0
Quote:
Originally Posted by Cheezeman
Tell me why please.
Probably because it is kind of slow, needs two locations, and can easily be replaced with coordinates. In fact, I don't need locations at all, except for GetLocationZ().
06-01-2009, 07:16 PM#11
Cheezeman
@grim001
As always, a very interesting answer. Made my mind going...

You say that by passing nothing to a function (as it takes nothing) it will not leak, or did I misunderstand you?
If I didn't, perhaps you could expand that topic a little more, because I don't understand the difference between Myfunction(null) and Myfunction(), since - to me - they both seem to leak.
Quote:
Originally Posted by akolyt0r
wtf no need to do this
Oh come on, seriously? Can't I post what I have in mind?
06-01-2009, 07:30 PM#12
grim001
Function parameters get their reference counters decremented when the function goes out of scope, unlike handle variables declared at the top of the function. The example I posted is using a wrapper function to call a function that takes a null unit so that the unit parameter exists and you don't need to declare it.
06-01-2009, 08:47 PM#13
Anitarf
Quote:
Originally Posted by Cheezeman
Tell me why please.
It uses locations. What's worse, it creates a location which you need to clean up. Everything that has to do with locations sucks as it is simply inferior to using coordinates directly (except in those two cases where for some reason there's no coordinate alternative, namely GetSpellTargetLoc and GetLocationZ).
06-01-2009, 11:15 PM#14
Feroc1ty
Quote:
Originally Posted by ploks
Can you use a temporary global?


Collapse JASS:
globals
private unit temp
endglobals

function A takes nothing returns unit
local unit u = CreateUnit()

//Do things
set temp = u
set u = null
return temp
endfunction

That's pretty stupid...

Collapse JASS:
function a takes nothing returns unit
set temp = CreateUnit(...)
return temp
endfunction
06-02-2009, 05:05 AM#15
ploks
Quote:
Originally Posted by Feroc1ty
That's pretty stupid...


Indeed it is. Sometimes the simplest way is hidden for your mind.