| 04-26-2009, 05:57 PM | #1 |
Hi guys, I am teaching student of mine into the world of vJass and good coding practice (as Anitarf and Pyro thought me). So I told my student that in a regular case of a spell, we can make our triggers local like this JASS:private function Init takes nothign returns nothing local trigger Trg = CreateTrigger() endfunction And I also said him, that we don't need to make JASS:set Trg = null However I have other guy, showing up in the thread, and he says I have to null all triggers because they are handles. We went though a discussion, and we didn't reach any solid conclusions ... So, I am asking here, what should I do? Should I null or not null the triggers? |
| 04-26-2009, 06:07 PM | #2 |
handle equals pointer in our case... the reason why we dont have to null triggers is, that they wont be destroyed ever ... except when you have dynamic triggers, but we shouldnt use those anyway... |
| 04-26-2009, 06:10 PM | #3 | |||
Quote:
First of all, always null handle local variables when they reach the end of their 'scope', always. Plenty of times, it is not necessary to null a local. But turning the nulling into a mechanical process while trivializing it is better than having to give thought to such a lame thing... Just to save a couple of lines of codee... However, if this is your typical spell trigger, it is true you don't have to null it. Quote:
The first thing to notice is that handle indexes are ref-counted , even if you call a Destructor (IE RemoveLocation) on a handle, its index will remain alive until all references against it are down. When you call a Destructor, a 4 bytes handle "ghost" will remain in its location. What happens here, is that there is a blizzard bug, references for local variables don't go down automatically when a function's scope ends. So we set them to null so the reference counter goes down, and the index can be recycled (and we can get those 4 bytes back) The bytes are not an issue unless you have this leak multiple times, however, the indexes are a problem. JASS:function err takes nothing returns nothing local location loc = Location(0, 4.5) call RemoveLocation(loc) endfunction This line of code leaks. We are removing the location object, however, the bug prevents wc3 to notice loc's index is no longer in use. And thus this index will leak. JASS:function err takes nothing returns nothing local location loc = Location(0, 4.5) call RemoveLocation(loc) set loc = null endfunction Anyway, if the handle held by the variable is not EVER destroyed, then you don't have to null it. That's the real reason that in the case you pointed, you don't need to null the variable. Quote:
Aw, give me a break, no, this doesn't matter at all. |
| 04-26-2009, 06:47 PM | #4 | |
Ahhh... Thx for explaining this detail to me guys! Quote:
Tell that to them, not to me xD |
| 04-26-2009, 07:03 PM | #5 |
Vex, I find it ironic that you ended your post about nulling permanent triggers with "this doesn't matter at all" (in another context). I should try to write a tool to insert the local-nulling automatically someday. It would just be a matter of identifying functions, finding the exit points, and assigning the locals to temp globals if they're needed for the return statement. |
| 04-26-2009, 09:42 PM | #6 |
So as soon as there are no more variables referencing a specific handle it will recycle the handle ID (given the actual handle has been destroyed/removed)? |
| 04-26-2009, 09:48 PM | #7 | |
Quote:
|
| 04-27-2009, 08:14 AM | #8 |
Now I am confused with a question .. as far as I understood, we only need to set something to null when we destroy it. If we never destroy it, we don't need to set it to null right? So, imagine I have a map with a unit called Chuck Norris. Chuck Norris is all mighty, all powerful and is also indestructible. Imagine also that Chuck Norris can cast a spell called roundhouse kick. So imagine this function JASS:private function Actions takes nothing returns nothing local unit God = GetTriggerUnit() //effect of the spell and destruction of the Universe as we know it... //end of spell //having in mind Chuck Norris will NEVER die not be destroyed, should I null the unit variable? endfunction So basically, after the destruction of the universe, and having in mind Chuck Norris is a unit that will never die, should I null him? PS: Any answer to this post is wrong, Chuck Norris level of awesomeness cannot be contained in any computer nor machine. Btw: http://www.chucknorrisfacts.com/ PS2: Please DO answer my question, I am kinda confused =S |
| 04-27-2009, 08:28 AM | #9 |
You are correct. You do not technically need to null 'God', but all the same it's good practice and it surely can't hurt you to. |
