| 06-01-2009, 08:56 AM | #1 |
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 |
PolarProjectionBJ doesn't leak on it's own. It just sucks. |
| 06-01-2009, 04:02 PM | #3 |
"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 | |
Quote:
|
| 06-01-2009, 05:50 PM | #5 |
Well you've noticed something that's correct... 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: 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 |
I didnt know that. Thats cool, and really nice and smooth actually. |
| 06-01-2009, 06:19 PM | #7 |
Can you use a temporary global? 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 |
Yes you can, it works too. ( Thats how I used to do it ) |
| 06-01-2009, 06:40 PM | #9 |
wtf no need to do this |
| 06-01-2009, 06:40 PM | #10 | |
Quote:
|
| 06-01-2009, 07:16 PM | #11 | |
@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:
|
| 06-01-2009, 07:30 PM | #12 |
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 | |
Quote:
|
| 06-01-2009, 11:15 PM | #14 | |
Quote:
That's pretty stupid... JASS:function a takes nothing returns unit set temp = CreateUnit(...) return temp endfunction |
| 06-02-2009, 05:05 AM | #15 | |
Quote:
Indeed it is. Sometimes the simplest way is hidden for your mind. |
