fix a use-after-free memory bug