Fix potential use-after-free.