fix use-after-free at exit