fix a potential use_after_free