fix use after free bug in the trigger handling code