HomeUser Control Panel (unavailable in archive)ForumsTutorialsArt GalleryResourcesMaps

Should I null triggers?

04-26-2009, 05:57 PM#1
Flame_Phoenix
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
Collapse 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
Collapse JASS:
set Trg = null
(in that case) because Trg is not really an object, it is a pointer, so it's leak can be compared to an integer's leak. This way if we null the trigger we will be actually making our code slower because we waste time with useless stuff.
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
akolyt0r
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
Vexorian
Quote:
So I told my student that in a regular case of a spell, we can make our triggers local like this
student? uh oh.

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:
in that case) because Trg is not really an object, it is a pointer, so it's leak can be compared to an integer's leak.
Wrong. First of all, you are not worried about the pointer itself leaking, the reason you null locals is because of the handle's index.

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.


Collapse 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.

Collapse JASS:
function err takes nothing returns nothing
 local location loc = Location(0, 4.5)
   call RemoveLocation(loc)
 set loc = null
endfunction
This does not leak anymore.

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:
This way if we null the trigger we will be actually making our code slower because we waste time with useless stuff.

Aw, give me a break, no, this doesn't matter at all.
04-26-2009, 06:47 PM#4
Flame_Phoenix
Ahhh... Thx for explaining this detail to me guys!

Quote:
Aw, give me a break, no, this doesn't matter at all.
Lol, quite funny, having in mind some people like Anitarf or HindyHat use that argument as a reason to "do not null local triggers".
Tell that to them, not to me xD
04-26-2009, 07:03 PM#5
Strilanc
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
TheKid
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
Anitarf
Quote:
Originally Posted by TheKid
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)?
It will recycle the id when it is needed: that is, when another handle needs to be created; but you are correct, in order for an id to be recycled, the handle stored there needs to be destroyed and it's reference counter needs to be at zero which means no variables are pointing to it (and no local variables have been pointing to it at the time their function returned).
04-27-2009, 08:14 AM#8
Flame_Phoenix
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
Collapse 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
Pyrogasm
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.