Fix memory leak in state machine. closes #1593