Fix SIGSEGV from use-after-free