fix possible use-after-free
fix possible use-after-free
plug memory leak in error code path