fix potential use-after-free