HomeUser Control Panel (unavailable in archive)ForumsTutorialsArt GalleryResourcesMaps

For the record - TriggerRemoveAction can be used to stop threads

06-05-2006, 10:35 PM#1
Blade.dk
I never knew this before. Maybe somebody else do, but I am posting it anyways, to all the guys who do not know it.
____

Destroying a trigger that is currently running using DestroyTrigger does not stop it from finishing executing.

But removing the triggeractions stops it instantly.

See attached demo map.

The Code

Collapse JASS:
function Count takes nothing returns nothing
    local integer i = 0
    loop
        call TriggerSleepAction(0)
        call BJDebugMsg(I2S(i))
        set i = i+1
    endloop
endfunction

function TestFunc takes nothing returns nothing
    local trigger c = CreateTrigger()
    local trigger e = CreateTrigger()
    local triggeraction a = TriggerAddAction(c, function Count)
    call BJDebugMsg("Press ESC to stop the thread using TriggerRemoveAction.")
    call TriggerExecute(c)
    call TriggerRegisterPlayerEvent(e, Player(0), EVENT_PLAYER_END_CINEMATIC)
    loop
        call TriggerSleepAction(0)
        exitwhen GetTriggerExecCount(e) > 0
    endloop
    call TriggerRemoveAction(c, a)
endfunction

//===========================================================================
function InitTrig_Test takes nothing returns nothing
    call ExecuteFunc("TestFunc")
endfunction

Attached Files
File type: w3mTriggerRemoveActionTest.w3m (12.2 KB)
06-06-2006, 02:36 PM#2
weaaddar
yes, I demonstrated this in my cinskipmadeEasy map. I'm right now exploring condition functions can they use waits?
06-06-2006, 02:41 PM#3
Vexorian
I swear I knew this already and reported it to either wc3jass.com or this site.

I considered it an annoyance cause it can't be practical sicne you can't recognize it in the trigger that is being stopped, so if you had any handle variables or objects you wanted to destroy they'll leak and all that kind of problems.

BTW: A trigger can have multiple actions
06-06-2006, 04:48 PM#4
iNfraNe
Wait a minute.. does this mean you cannot fully clean up the action inside the action itself?
Since removing the action will.. stop the action, which means the variable cannot be set to null? Only way out I see is return bug..
06-06-2006, 06:09 PM#5
Blade.dk
You can't clean stuff up in the end of the thread, as it stops instantly.

I considered it an annoyance too when I found out about it, but I can think of a couple of situations now where it could be useful.
06-06-2006, 06:52 PM#6
Anitarf
So, for cleaning up a trigger (for example a dynamicaly created trigger that destroys itself once executed), you must first start a seperate thread with ExecuteFunc and then clean up your trigger from there?

Edit: has it been confirmed that trigger actions leak if they're not destroyed seperately from the trigger? I remember Vex once stating that he removes triggeractions just as a precaution, and that he wasn't sure if it leaked at all and if perhaps DestroyTrigger was enough...
06-06-2006, 07:18 PM#7
Blade.dk
If you are going to remove the triggeraction from inside the action function, then no cleanup after that will take place, so another thread is best there.

I have not tested it, but triggeractions are new objects created everytime, it would be very weird if they did not leak, like other dynamically created objects.
06-07-2006, 12:38 AM#8
PipeDream
Yes, triggeractions require triggerremoveaction for deallocation.
This explains why it appeared impossible to make triggers with out leaking.. really good to know.
06-07-2006, 12:44 AM#9
Vexorian
back then I wasn't sure if TriggerRemoveAction was enough I knew that I had to clean the actions but wadn't sure if that's the fix for all leaks when creating triggers. Right now it seems to work quite well.