apphook: fix potential use-after-free at exit